Forum: Ruby on Rails Stories with many tags, tags with many stories, has_and_belo

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 =?ISO-8859-1?Q?Iv=E1n_Vega_Rivera?= (Guest)
on 2005-12-23 03:01
(Received via mailing list)
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.
137039860fa6b38713e1349da8a4137d?d=identicon&s=25 Marc Love (Guest)
on 2005-12-23 07:55
While the acts_as_taggable mixin might work for you here, it should also
be noted that ActiveRecord's latest version includes the ability to
create find_or_create_by_X methods.  Because sometimes you want to do a
find or create if doesn't exist for things other than tag models.

See:
http://weblog.rubyonrails.com/articles/2005/11/07/...


=?ISO-8859-1?Q?Iv=E1n_Vega_Rivera?= wrote:
> 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.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 i.v.r. (Guest)
on 2005-12-23 18:44
(Received via mailing list)
That will come in handy too! Thanks
This topic is locked and can not be replied to.