Has many through

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

can you post the table definition?

On Oct 20, 11:15 pm, Scott H. [email protected]

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

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Scott H. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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:

…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 H. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

It seems so simple but it’s driving me insane! Please help :slight_smile:

On 13 November 2012 13:56, Werner [email protected]
wrote:

Hi Colin.

Please remember to reply to the list and please don’t top post, it
makes it difficult to follow the thread. Insert your reply inline at
appropriate points in the previous message. Thanks.

  1. I ask for entries in booking with a certain project_id
    @bookings = Booking.find_all_by_project_id(params[:id])
    => [#<Booking id: 1, week_id: 47, project_id: 2, hour: 4>, #<Booking id: 2,

week_id: 48, project_id: 2, hour: 7>]

So you know how to do that ok?

  1. Get the values from the table week which have the week_id from the 1 (47,

Sorry, still don’t understand what that is supposed to mean. I
presume that you mean records rather than values and the table weeks
rather than weeks. So you want to get the records from the table
weeks. But that table does not have a column week_id, just id and no
idea what you mean by “from the 1(47,48)”

Colin

On 13 November 2012 15:15, Colin L. [email protected] wrote:

=> [#<Booking id: 1, week_id: 47, project_id: 2, hour: 4>, #<Booking id: 2,
rather than weeks. So you want to get the records from the table
weeks. But that table does not have a column week_id, just id and no
idea what you mean by “from the 1(47,48)”

Ah, I think I understand. You have a project_id in params[:id] and
you want to find the bookings and hence the weeks for that project id?

Why don’t you just get the project for the id
@project = Project.find params[:id]
then the bookings, if you actually need the bookings
@bookings = @project.bookings
and/or the weeks by
@weeks = @project.weeks

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs