4

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 (i.e. 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 ?

5
  • 1
    Try giving it like this e = Company.joins(:users).select("companies.company_name, users.name") Commented Jun 12, 2014 at 12:04
  • Try companies = Company.select("companies.company_name AS company_name, users.name AS user_name").joins(:users) and for displaying: companies.each{ |c| puts c.company_name, c.user_name } Commented Jun 12, 2014 at 12:16
  • maybe even e = Company.joins(:users).select("companies.company_name, users.name") works based on guides.rubyonrails.org/… Commented Jun 12, 2014 at 12:19
  • @pavan, thanks for the suggestion. As you may see below, the problem was not how I formulated the query but rather how I treated its results. Commented Jun 12, 2014 at 14:02
  • @Octopus-Paul: Thanks for the suggestion, it is in line with John 's answer below. Commented Jun 12, 2014 at 14:04

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

@John, thanks a lot. I was really getting frustrated by this issue. It is exactly as you said, the way I did the query was allright, I just did not realise that the response one gets in Rails console does not really reflect the result of the query that is stored in the variable.... Seems I'm still at the bottom of the Rails learning curve.
It just runs a script under a rails environment @arup
It has caught me before too @user. For some reason the other columns are ignored by inspect, but they are there!
Can I run any .rb file inside rails env ?
Yes you can use it to run cron jobs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.