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