I can''t seem to find any examples of how to do this so I am hoping one of you can shed some light. I have three models: Person has_many :is_looking_for IsLookingFor belongs_to :person belongs_to :quality Book has_many :is_looking_for A user has just entered a new book and I want the book to be created, if it does not exists and it be associated to him. Anyone have any ideas how this works? I can do it in traditional SQL but I want to do it in the Rails way because I am sure their is an easy and sweet way of doing it.
I''m working on something very similar right now. I haven''t got to the point you''re at yet (needing to add to a model acting as a join table), but here''s what I''d try first: Supposing @user is a Person: @book = Book.create @looking = IsLookingFor.new( :person_id => @user.id, :book_id => @book.id ) @looking.save I''m assuming that IsLookingFor belongs_to :quality is a typo. Is that the case? Duane Johnson (canadaduane) On May 27, 2005, at 3:22 PM, John Kopanas wrote:> I can''t seem to find any examples of how to do this so I am hoping > one of you can shed some light. > > I have three models: > > Person > has_many :is_looking_for > > IsLookingFor > belongs_to :person > belongs_to :quality > > Book > has_many :is_looking_for > > A user has just entered a new book and I want the book to be > created, if it does not exists and it be associated to him. > > Anyone have any ideas how this works? I can do it in traditional > SQL but I want to do it in the Rails way because I am sure their is > an easy and sweet way of doing it. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Wow... so simple... very impressive. On 27-May-05, at 5:44 PM, Duane Johnson wrote:> I''m working on something very similar right now. I haven''t got to > the point you''re at yet (needing to add to a model acting as a join > table), but here''s what I''d try first: > > Supposing @user is a Person: > > @book = Book.create > @looking = IsLookingFor.new( :person_id => @user.id, :book_id => > @book.id ) > @looking.save > > I''m assuming that IsLookingFor belongs_to :quality is a typo. Is > that the case? > > Duane Johnson > (canadaduane) > > On May 27, 2005, at 3:22 PM, John Kopanas wrote: > > >> I can''t seem to find any examples of how to do this so I am hoping >> one of you can shed some light. >> >> I have three models: >> >> Person >> has_many :is_looking_for >> >> IsLookingFor >> belongs_to :person >> belongs_to :quality >> >> Book >> has_many :is_looking_for >> >> A user has just entered a new book and I want the book to be >> created, if it does not exists and it be associated to him. >> >> Anyone have any ideas how this works? I can do it in traditional >> SQL but I want to do it in the Rails way because I am sure their >> is an easy and sweet way of doing it. >> _______________________________________________ >> 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 >
Wouldn''t it be more concise to use Person has_and_belongs_to_many: books Book has_and_belongs_to_many:people> > > > Person > > has_many :is_looking_for > > > > IsLookingFor > > belongs_to :person > > belongs_to :quality > > > > Book > > has_many :is_looking_for > > > > A user has just entered a new book and I want the book to be > > created, if it does not exists and it be associated to him. > > > > Anyone have any ideas how this works? I can do it in traditional > > SQL but I want to do it in the Rails way because I am sure their is > > an easy and sweet way of doing it. > > _______________________________________________ > > 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 used to have it that way but I realized that the relationship had some data requirements so I added a model between the two. On 27-May-05, at 6:10 PM, Barry Walker wrote:> Wouldn''t it be more concise to use > > Person > has_and_belongs_to_many: books > > Book > has_and_belongs_to_many:people > > >>> >>> Person >>> has_many :is_looking_for >>> >>> IsLookingFor >>> belongs_to :person >>> belongs_to :quality >>> >>> Book >>> has_many :is_looking_for >>> >>> A user has just entered a new book and I want the book to be >>> created, if it does not exists and it be associated to him. >>> >>> Anyone have any ideas how this works? I can do it in traditional >>> SQL but I want to do it in the Rails way because I am sure their is >>> an easy and sweet way of doing it. >>> _______________________________________________ >>> 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 >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >