Has many through

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.

--wpd

Best,

...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

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 problems

Best,

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 :slight_smile:

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.notes

You 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,