0

I've this models inmy code:

car.rb, model.rb, brand.rb

car belongs_to model
model belongs_to brand

I do this request :

select('brands.label, brands.id, count(ads.model_id) AS nbr_car').order('nbr_car DESC').joins(:model).joins('INNER JOIN brands AS brands ON brands.id = models.brand_id').group('brands.id').published.map{|c| [c.label, c.id]}

Which results in follwing SQL :

SELECT brands.label, brands.id, count(ads.model_id) AS nbr_car 
FROM `ads` 
INNER JOIN `models` ON `models`.`id` = `ads`.`model_id` 
INNER JOIN brands AS brands ON brands.id = models.brand_id 
WHERE `ads`.`type` IN ('Car') 
GROUP BY brands.id 
ORDER BY nbr_car DESC

In rails, I am getting empty array, but from SQL I am getting results!

What is wrong?

1
  • 2
    Are you sure you are running those against the same database? Commented Apr 24, 2014 at 14:03

1 Answer 1

1

I think the culprit can be published here, for which I could not found any counterpart in you SQL query. Try to print result of following query:

select('brands.label, brands.id, count(ads.model_id) AS nbr_car').
order('nbr_car DESC').joins(:model).joins('INNER JOIN brands AS brands ON brands.id = models.brand_id').
group('brands.id')
Sign up to request clarification or add additional context in comments.

Comments

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.