How to query for Companies with a certain Branch in a “has_many :through” relationship?
#company.rb
has_many :branch_choices
has_many :branches, :through => :branch_choices
“Find all companies with Branch ID 3”
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
or
UPDATE
Actually, there’s one downside to the first snippet: it eagerly loads branches along with the companies. To avoid this overhead you may consider using a left join: