Hello, Let me setup the scenario class User < ActiveRecord::Base belongs_to :house end class House < ActiveRecord::Base has_many :users end My question is, how can i implement the roomates association from the user model? I''d like to do current_user.roomates and get a list of current_user.house.users *minus* the current_user. Thanks! -Adam -- 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 -~----------~----~----~----~------~----~------~--~---
@House.find(:first).users - [current_user] in your controller somewhere. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Haven''t tested this but I''m thinking something like: class User < ActiveRecord::Base belongs_to :house def roommates User.find(:all, :conditions => [''house.id = ? and user.id <> ?'', self.house, self], :joins => "inner join houses on house.user_id users.id") end end On Jul 3, 6:06 pm, Adam Walters <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > Let me setup the scenario > > class User < ActiveRecord::Base > belongs_to :house > end > > class House < ActiveRecord::Base > has_many :users > end > > My question is, how can i implement the roomates association from the > user model? I''d like to do current_user.roomates and get a list of > current_user.house.users *minus* the current_user. > > Thanks! > > -Adam > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> @House.find(:first).users - [current_user] > > in your controller somewhere.I would put it in the models if i was to use a method like this class User < AR::Base belongs_to :house def roomates house.users - [self] end end the problem with this aproach is that you cant do anything with the rooomates apart from seeing them. you have no User.roomates << user.new or any of the Ar methods. So... I would put it into the association, having house as a join table between Users and Users... something on the lines (and note i say on the lines because you would probably have to change the associations you have to accomplish this.) of: class user has_many :roomates, :through => :house, :class_name => ''User'' end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---