In my application there are four models: Units: id, name (has_many :rooms) Rooms: id, name (has_many :rentals, belongs_to :units) People: id, name (has_many :rentals) Rentals: room_id, people_id, created_at, ended_at (belongs_to :rooms, :people) Rooms may have more than one person, but a person can only live in one room (as defined by a NULL ended_at). Before a person can move into a new room, s/he must "move_out" by setting ''ended_at.'' This I can deal with. I want to list the rooms and, if occupied, the name(s) of their people. Here are my attempts: @unit = Unit.find(params[:id]) @rooms = @unit.rooms @people = @unit.rooms.people gives --> undefined method `people'' for Room:Class @unit = Unit.find(params[:id]) @rooms = @unit.rooms @people = @unit.rooms.rentals.people gives --> undefined method `rentals'' for Room:Class What am I doing wrong? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
When you have a has_many then unit.rooms will return an array of rooms (well a collection proxy that looks like one), which doesn''t have a rentals method. Each object in that collection does, so you can get all rentals for your unit by iterating over @unit.rooms Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Rooms: id, name (has_many :rentals, belongs_to :units) > People: id, name (has_many :rentals) > Rentals: room_id, people_id, created_at, ended_at (belongs_to :rooms,:people) So Rentals is a join table between Rooms 6 people, correct? try this: Rooms: has_many :rentals has_many :people, :through => :rentals People: has_many :rentals has_many: rooms, :through => :rentals that should enable you to do @unit.rooms.people --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Rooms: > has_many :rentals > has_many :people, :through => :rentals > > People: > has_many :rentals > has_many: rooms, :through => :rentals > > that should enable you to do @unit.rooms.peopleI still get an undefined method for Room. Any other suggestions? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
THE CLASSES class Unit < ActiveRecord::Base has_many :rooms end class Room < ActiveRecord::Base belongs_to :units has_many :rentals has_many :people, :through => :rentals end class Person < ActiveRecord::Base has_many :rentals has_many :rooms, :through => :rentals end class Rental < ActiveRecord::Base belongs_to :rooms, :people end THE ACTION def show @unit = Unit.find(params[:id]) @rooms = @unit.rooms @people = @unit.rooms.people end THE ERROR NoMethodError in UnitsController#show undefined method `people'' for Room:Class -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
A few fixes, but still same error: all singular / pluralization issues have been fixed on the Classes. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Can''t do that. Proxies only work one level deep. When you think about it, @unit.rooms returns a bunch of Room objects. Invoking a single association on a collection of objects doesn''t make sense. You need to iterate them. At least that''s how it looks to me from your class definitions. Jacquie Fan wrote:> > > THE CLASSES > class Unit < ActiveRecord::Base > has_many :rooms > end > > > class Room < ActiveRecord::Base > belongs_to :units > has_many :rentals > has_many :people, :through => :rentals > end > > > class Person < ActiveRecord::Base > has_many :rentals > has_many :rooms, :through => :rentals > end > > > class Rental < ActiveRecord::Base > belongs_to :rooms, :people > end > > > THE ACTION > def show > @unit = Unit.find(params[:id]) > @rooms = @unit.rooms > @people = @unit.rooms.people > end > > > THE ERROR > NoMethodError in UnitsController#show > undefined method `people'' for Room:Class > > -- > Posted via http://www.ruby-forum.com/. > > > > >-- View this message in context: http://www.nabble.com/-Rails--Undefined-method-with-Active-Record-joins-tf2615629.html#a7312225 Sent from the RubyOnRails Users mailing list archive at Nabble.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Steve Ross wrote:> Can''t do that. Proxies only work one level deep. When you think about > it, > @unit.rooms returns a bunch of Room objects. Invoking a single > association > on a collection of objects doesn''t make sense. You need to iterate them. > > At least that''s how it looks to me from your class definitions.I played with it a while but couldn''t get anywhere. How can I grab the person''s name and id based on the unit? Maybe I should give up on using Rentals as a model and use a people_rooms join table instead? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---