简体   繁体   中英

rails query for attributes from different tables/models

I am aware that similar questions have been posted in the past and I have tried to find a solution in the rails guides or in answers to previous posts, but either the answers did not work for me, or they were not what I was looking for. I have previous SQL experience (mainly MySQL) and I am currently exploring Rails for a new project.

I have a table "users" and a table "companies". A user always belongs to only one company, but a company can have many users. a simple representation of my data is:

users has the following attributes: id (INT), name(string), email(string), company_id companies has the following attributes: id (INT), company_name(string), address(string)

When I execute the following SQL query directly in the database (not in rails):

SELECT users.name, company.address FROM companies INNER JOIN users on users.company_id=companies.id

I get a 2 column reply with the users.name in the first and the company.adres to which this user belongs in the second column. As it is an inner join, companies without users or users without companies are not listed, wich is exactly what I need.

Now trying to "translate" this query into rails (I am using Rails 4.1.1), I first defined the User model and the Company model, thus creating their respective user.rb and company.rb in the models directory. For the user.rb I declared "belongs_to :company" and for the company.rb I declared "has_many :users".

After migrating etc... I use the rails console to add some data and test. All queries on single models work fine, also queries like

Users.find(1).company.address

work fine, but when I type in the following command:

e = Company.select("companies.company_name, users.name").joins(:users)

it shows that the following SQL query is generated by rails:

SELECT companies.company_name, users.name FROM "companies" INNER JOIN "users" ON "users"."company_id" = "companies"."id"

which I suppose is the same query as the one I use when I access the DB directly. After showing the query, it shows the result, but the users.name attribute is not given. The result is an array with the correct number of elements (ie no users without company or no companies without users are shown), but only the attribute for the Company model is given, the user.name attribute is missing:

#<ActiveRecord::Relation [#<Company id: nil, address: "addresexample1">, #<Company id: nil, address: "addresexample3">, #<Company id: nil, address: "addressexample4">]>

Am I doing something wrong, or do I expect something that is not correct in Rails ?

I think you will find that it's actually there but you might not be able to see it...

Try something like this:

e = Company.select("companies.*, users.name as user_name").joins(:users)
e.each do |c|
   puts "company: #{c.name}, user: #{c.user_name}"
end

Output:

rails runner junk.rb
company: aaaaa, user: user 1
company: bbbbb, user: user 2
company: aaaaa, user: user 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM