Hi there, I want to do this, but it''s driving me nuts: event.guestlists.notes i.e. to find all the notes from all the guestlists from a particular event. class Guestlist < ActiveRecord::Base belongs_to :event has_many :notes end class Event < ActiveRecord::Base has_many :guestlists has_many :notes, :through => :guestlists end class Note < ActiveRecord::Base belongs_to :guestlist belongs_to :event end Any ideas what I''m doing wrong? Thanks!! Scott -- Posted via http://www.ruby-forum.com/.
can you post the table definition? On Oct 20, 11:15 pm, Scott Holland <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > > I want to do this, but it''s driving me nuts: > > event.guestlists.notes > > i.e. to find all the notes from all the guestlists from a particular > event. > > class Guestlist < ActiveRecord::Base > belongs_to :event > has_many :notes > end > > class Event < ActiveRecord::Base > has_many :guestlists > has_many :notes, :through => :guestlists > end > > class Note < ActiveRecord::Base > belongs_to :guestlist > belongs_to :event > end > > Any ideas what I''m doing wrong? > > Thanks!! > > Scott > -- > Posted viahttp://www.ruby-forum.com/.
guestlists is plural. event.guestslists returns a list of Guestlist records. If you want all of the notes for all of the guestlists for a particular event, (as an array) you might try something like: event.guestlists.collect(&:notes).flatten A true Rubyista could probably tell you a much more elegant way to do this. --wpd
Patrick Doyle wrote:> guestlists is plural. > > event.guestslists returns a list of Guestlist records. > > If you want all of the notes for all of the guestlists for a > particular event, (as an array) you might try something like: > > event.guestlists.collect(&:notes).flatten > > A true Rubyista could probably tell you a much more elegant way to do > this....which would be event.notes , since event has_many notes through guestlists.> > --wpdBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
> ...which would be event.notes , since event has_many notes through > guestlists.An event also has notes, so that gives me just the notes for the event. In my notes table I have: event_id (if the note is about an event) guestlist_id (if the note is about a guestlist) I need to seperate the notes out, which is where I am having problems -- Posted via http://www.ruby-forum.com/.
Scott Holland wrote:> >> ...which would be event.notes , since event has_many notes through >> guestlists. > > An event also has notes, so that gives me just the notes for the event. > > In my notes table I have: > > event_id (if the note is about an event) > > guestlist_id (if the note is about a guestlist)Then your associations, as given in your original posts, do not match your DB schema. According to your DB schema, you don''t need has_many :through; instead, you need a polymorphic association such that a note can belong to either a guestlist or an event.> > I need to seperate the notes out, which is where I am having problemsBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
> Then your associations, as given in your original posts, do not match > your DB schema. According to your DB schema, you don''t need has_many > :through; instead, you need a polymorphic association such that a note > can belong to either a guestlist or an event.Ok, so now I have the polymorphic relationship set up and working fine... class Event < ActiveRecord::Base has_many :guestlists has_many :notes, :as => :notable end class Guestlist < ActiveRecord::Base belongs_to :event has_many :notes, :as => :notable end class Note < ActiveRecord::Base belongs_to :notable, :polymorphic => true end so event.notes works fine, and guestlist.notes works fine, but how do I get: event.guestlists.notes It seems so simple but it''s driving me insane! Please help :-) -- Posted via http://www.ruby-forum.com/.
Scott Holland wrote: [...].> > Ok, so now I have the polymorphic relationship set up and working > fine... > > > class Event < ActiveRecord::Base > has_many :guestlists > has_many :notes, :as => :notable > end > > class Guestlist < ActiveRecord::Base > belongs_to :event > has_many :notes, :as => :notable > end > > class Note < ActiveRecord::Base > belongs_to :notable, :polymorphic => true > end > > so event.notes works fine, and guestlist.notes works fine, but how do I > get: > > event.guestlists.notesYou don''t. @event.guestlists is an Array, so it has no #notes method. You''ll need to either use @event.guestlists.collect{|g| g.notes}.flatten (more ORM-ish, less efficient) or Note.find(:all, :conditions => {:notable_id => @event.guestlist_ids, :notable_type => ''Guestlist''}) (more efficient) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > It seems so simple but it''s driving me insane! Please help :-)-- Posted via http://www.ruby-forum.com/.