Question: company_user belongs to user, and belongs to company. Then, document belongs to company. I want to take a user, and grab all of the companies he belongs to, and also grab all of the documents of those companies. So I want to take user_id, grab the corrosponding company_users, then from there use the company_users'' company_id to grab the companies, then from there use the company_id in documents to match up with those companies and grab them. How do I do that? Thanks for your time! Ben Lisbakken --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
First off you want class Company < ActiveRecord::Base has_and_belongs_to_many :users # ... end class User < ActiveRecord::Base has_and_belongs_to_many :companies # ... end user = User.find(params[:id]) # assuming a request companies = user.companies user_documents = {} companies.each { |company| user_documents[company.name] = company.documents # use whatever key is convenient for you } --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/21/06, Ben Lisbakken <lisbakke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Question: > company_user belongs to user, and belongs to company. Then, document > belongs to company.I assume you have models that look something like this. class User < ActiveRecord::Base has_many :company_users has_many :companies, :through => :company_users end class Company < ActiveRecord::Base has_many :company_users has_many :users, :through => :company_users has_many :documents end class CompanyUser < ActiveRecord::Base belongs_to :user belongs_to :company end class Document < ActiveRecord::Base belongs_to :company end> I want to take a user, and grab all of the companies he belongs to, and > also grab all of the documents of those companies.If you have a user, you should be able to simply do the following. companies = user.companies if companies.nil? # deal with the case where no companies were found else for company in companies documents = company.documents # Do whatever with documents. end end -- James --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I haven''t read your responses, but here are my models. I''ll read them in just a sec... thanks for the quick and great feedback! class User < ActiveRecord::Base has_many :company_users end class CompanyUser < ActiveRecord::Base belongs_to :company belongs_to :user end class Company < ActiveRecord::Base has_many :company_users has_many :documents end class Document < ActiveRecord::Base belongs_to :company end -Ben --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Cool I read both of yours... James yours is more like what I did. I was trying to see if this could be done in one sql statement to grab alllll of it. Brian, would you suggest I get rid of the company_users table? What fields would I need to add to companies and users? Thanks again guys! Ben --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oops I was confused. Leaving company_users in, and using :through. Can I do habtm :through? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
errr no.. don''t get rid of the company_user table. You need it. has_and_belongs_to_many is used for pure join tables where it contains just two foreign keys has_many :through is for join tables that contain additional data, i.e. it''s intended to be used as another full-fledged model Don''t think in terms of SQL anymore. Let rails handle it. The associations are what let rails automagically generate the SQL statements for you. If you tail -f your development.log you''ll see. On 9/21/06, Ben Lisbakken <lisbakke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Cool I read both of yours... James yours is more like what I did. I > was trying to see if this could be done in one sql statement to grab > alllll of it. > > Brian, would you suggest I get rid of the company_users table? What > fields would I need to add to companies and users? > > Thanks again guys! > Ben > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/21/06, Ben Lisbakken <lisbakke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Brian, would you suggest I get rid of the company_users table? What > fields would I need to add to companies and users?You need the table. What you might not need to do is explicitly define the model. That''s the main difference between Brian''s code and the code I posted. If companies_users stores only the appropriate foreign keys, then use Brian''s method. If you need to store more info for a user-company relationship, then my way will work. -- James --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 22/09/2006, at 8:03 AM, Ben Lisbakken wrote:> I > was trying to see if this could be done in one sql statement to grab > alllll of it.It can. Using either Brian''s or James'' setup, you can do: user.companies.find(:all, :include => :documents) Pete Yandell --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I have a model User who has many Posts I now want a user to be able to group one of his posts with any posts from any user. so i have a Groups model. in the groups database i have: user_id and post_id Groups: has_one :user has_one :post Posts: has_many :groups so i pull out a post from a user. how can i find what other posts are grouped to that post? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---