I began experimenting with habtm relationships, and so I created a
stories table, a tags table, and a stories_tags table referencing the
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
tags = Array.new
count = 0
taglist.split(/,/).each do |tag|
tags[count] = Tag.new(:name => tag.chop.downcase)
count += count
class StoriesController < ApplicationController
@tags = Tag.create(params[:tags][:tags])
params[:story][:tags] = @tags
@story = Story.new(params[:story])
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?