[Rails] Stories with many tags, tags with many stories,
has_and_belongs_to_many howto?
Iván Vega Rivera
ivanvega at gmail.com
Fri Dec 23 02:01:54 GMT 2005
Hi,
I began experimenting with habtm relationships, and so I created a
stories table, a tags table, and a stories_tags table referencing the
former 2.
When I create a "story", I want to add several tags to it. Then, the
model should create those tags and link them to the respective stories,
and for the tags that already exists, just link them to the story.
I tried something like this (snippets):
class Tag < ActiveRecord::Base
def self.create(taglist)
tags = Array.new
count = 0
taglist.split(/,/).each do |tag|
tags[count] = Tag.new(:name => tag.chop.downcase)
count += count
end
end
end
class StoriesController < ApplicationController
def create
@tags = Tag.create(params[:tags][:tags])
params[:story][:tags] = @tags
@story = Story.new(params[:story])
render_text @tags.length
end
end
params[:tags][:tags] can be something like "cats, dogs, pets". With that
code, I get: "Tag expected, got String".
I suppose I could save each tag individually, retrieve it's id, and
create the rows in the link table, myself, but this is Rails, so I'm
sure there's a much better way.
Could you help me out?
Thanks!
Ivan V.
More information about the Rails
mailing list