Forum: Ruby on Rails Has many through

Posted by Scott Holland (ginsberg1982)
on 2009-10-20 23:15
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 by Jose Ernesto Suarez (Guest)
on 2009-10-21 01:14
(Received via mailing list)
can you post the table definition?

On Oct 20, 11:15 pm, Scott Holland <rails-mailing-l...@andreas-s.net>
Posted by Patrick Doyle (Guest)
on 2009-10-21 01:15
(Received via mailing list)
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
Posted by Marnen Laibow-Koser (marnen)
on 2009-10-21 01:29
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,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Scott Holland (ginsberg1982)
on 2009-10-21 08:51
> ...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 by Marnen Laibow-Koser (marnen)
on 2009-10-21 09:54
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,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Scott Holland (ginsberg1982)
on 2009-10-24 14:06
> 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 by Marnen Laibow-Koser (marnen)
on 2009-10-25 00:23
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,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
> 
> It seems so simple but it's driving me insane! Please help :-)
Posted by Colin Law (Guest)
on 2012-11-14 09:19
(Received via mailing list)
On 13 November 2012 13:56, Werner <webagentur.laude@googlemail.com> 
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?

>
> 2. Get the values from the table week which have the week_id from the 1 (47,
> 48)

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
Posted by Colin Law (Guest)
on 2012-11-14 10:05
(Received via mailing list)
On 13 November 2012 15:15, Colin Law <clanlaw@googlemail.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.