I have a model Person and one Organisation. A Person can belong to any number of Organisations and an Organisation can have any number of Persons attributed to it. As below: ===class Person < ActiveRecord::Base has_many :person_organisations, :dependent => true has_many :organisations, :through => :person_organisations end class Organisation < ActiveRecord::Base has_many :person_organisations, :dependent => true has_many :persons, :through => :person_organisations end === I''m writing an action on my Person controller called ''list_by_organisation'' which will return all the contacts from a specified organisation. How do I achieve this? Do I do something like the following? But what goes in the condition? === def list_by_organisation @person_pages, @persons = paginate(:persons, :per_page => 20, :order => ''id'', :conditions => [???]) render_action ''list'' end === If I''m doing this in completely the wrong way then please do let me know. Thanks for any 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 -~----------~----~----~----~------~----~------~--~---
Matthew Planchant wrote:> I have a model Person and one Organisation. A Person can belong to any > number of Organisations and an Organisation can have any number of > Persons attributed to it. As below: > > ===> class Person < ActiveRecord::Base > has_many :person_organisations, :dependent => true > has_many :organisations, :through => :person_organisations > end > > class Organisation < ActiveRecord::Base > has_many :person_organisations, :dependent => true > has_many :persons, :through => :person_organisations > end > ===> > I''m writing an action on my Person controller called > ''list_by_organisation'' which will return all the contacts from a > specified organisation. How do I achieve this? > > Do I do something like the following? But what goes in the condition? > > ===> def list_by_organisation > @person_pages, @persons = paginate(:persons, :per_page => 20, :order > => ''id'', :conditions => [???]) > render_action ''list'' > end > ===> > If I''m doing this in completely the wrong way then please do let me > know. > > Thanks for any suggestions. > >You need either has_and_belongs_to_many (if the join table has no attributes), in which case you don''t need :through (AR does all that for you), or if the join table has attributes (e.g. date of joining) you need to declare that as a model (in which case I''d prob just call it membership). In your Person controller you could do something like @organisations = Organisation.find(:all, :include => :persons) if you want all the organisations, and then in your view iterate through them: <% @organisations.each do |organisation| %> <h1><%= h(organisation.title) %></h1> organisation.persons.each do |person| <p><%= h(person.name) %></p> <% end %> <% end %> or if you just want members of a specific organisation (identified by, say, params[:organisation_id]) @persons = Organisation.find(params[:organisation_id]).persons Or something like that. HTH CT p.s. I think rails by default uses people as the plural of persons (so should be has_many :people, not has_many :persons, ditto all the other persons) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> You need either has_and_belongs_to_many (if the join table has no > attributes), in which case you don''t need :through (AR does all that for > you), or if the join table has attributes (e.g. date of joining) you > need to declare that as a model (in which case I''d prob just call it > membership).Yep.> In your Person controller you could do something like @organisations > Organisation.find(:all, :include => :persons) if you want all the > organisations, and then in your view iterate through them: > > <% @organisations.each do |organisation| %> > <h1><%= h(organisation.title) %></h1> > organisation.persons.each do |person| > <p><%= h(person.name) %></p> > <% end %> > <% end %> > > or if you just want members of a specific organisation (identified by, > say, params[:organisation_id]) > @persons = Organisation.find(params[:organisation_id]).persons > > Or something like that.OK. Thanks. So what is wrong with: @person_pages, @people = paginate(:people, :per_page => 20, :order => ''id'', :include => :organisations, :conditions => [''organisations.id=?'', params[:id]])> p.s. I think rails by default uses people as the plural of persons (so > should be has_many :people, not has_many :persons, ditto all the other > persons)Yep I''ve realised this. The above is just an example though. -- 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 -~----------~----~----~----~------~----~------~--~---
Chris T wrote:> or if you just want members of a specific organisation (identified by, > say, params[:organisation_id]) > @persons = Organisation.find(params[:organisation_id]).personsThat worked many thanks! -- 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 -~----------~----~----~----~------~----~------~--~---