Iván Vega Rivera
2005-Dec-23 02:01 UTC
Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
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.
Tom Fakes
2005-Dec-23 03:00 UTC
RE: Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
Check out the acts_as_taggable mixin here: http://rubyforge.org/projects/taggable/ and http://dema.ruby.com.br/articles/2005/09/03/tagging-on-steroids-with-rails -----Original Message----- From: Iván Vega Rivera [mailto:ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] Sent: Thursday, December 22, 2005 6:02 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] Stories with many tags, tags with many stories, has_and_belongs_to_many howto? 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. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2005-Dec-23 04:18 UTC
Re: Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
Yes check out acts_as_taggable you will like it. But one error I see in your self.create method may be holding you back./ You initially set the count to 0 then you try to increment it in the each iterator by doing count += count. Well this just adds 0 + 0 and never increases your count. Cheers- -Ezra On Dec 22, 2005, at 7:00 PM, Tom Fakes wrote:> Check out the acts_as_taggable mixin here: http://rubyforge.org/ > projects/taggable/ and http://dema.ruby.com.br/articles/2005/09/03/ > tagging-on-steroids-with-rails > > > -----Original Message----- > From: Iván Vega Rivera [mailto:ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] > Sent: Thursday, December 22, 2005 6:02 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] Stories with many tags, tags with many stories, > has_and_belongs_to_many howto? > > 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. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Marc Love
2005-Dec-23 06:55 UTC
Re: Stories with many tags, tags with many stories, has_and_
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/rails-1-0-rc4-0-14-3-its-the-final-countdown =?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.-- Posted via http://www.ruby-forum.com/.
i.v.r.
2005-Dec-23 17:43 UTC
Re: Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
Ah, yes. That should''ve been count += 1, heh... Thanks! Ezra Zygmuntowicz wrote:> Yes check out acts_as_taggable you will like it. But one error I see > in your self.create method may be holding you back./ You initially set > the count to 0 then you try to increment it in the each iterator by > doing count += count. Well this just adds 0 + 0 and never increases > your count. > > Cheers- > > -Ezra > > On Dec 22, 2005, at 7:00 PM, Tom Fakes wrote: > >> Check out the acts_as_taggable mixin here: >> http://rubyforge.org/projects/taggable/ and >> http://dema.ruby.com.br/articles/2005/09/03/tagging-on-steroids-with-rails >> >> >> >> -----Original Message----- >> From: Iván Vega Rivera [mailto:ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] >> Sent: Thursday, December 22, 2005 6:02 PM >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: [Rails] Stories with many tags, tags with many stories, >> has_and_belongs_to_many howto? >> >> 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. >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > 509-577-7732 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
i.v.r.
2005-Dec-23 17:43 UTC
Re: Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
Thanks a lot, that seems pretty useful! Tom Fakes wrote:> Check out the acts_as_taggable mixin here: http://rubyforge.org/projects/taggable/ and http://dema.ruby.com.br/articles/2005/09/03/tagging-on-steroids-with-rails > > > -----Original Message----- > From: Iván Vega Rivera [mailto:ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] > Sent: Thursday, December 22, 2005 6:02 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] Stories with many tags, tags with many stories, has_and_belongs_to_many howto? > > 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. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
i.v.r.
2005-Dec-23 17:45 UTC
Re: Re: Stories with many tags, tags with many stories, has_and_
That will come in handy too! Thanks Marc Love wrote:> 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/rails-1-0-rc4-0-14-3-its-the-final-countdown > > > =?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. >> > > >