Q: ActiveRecord: find data from has_many relationships I am trying to find records that have a has_many relationship in rails using activerecord.  I am having a difficult time phrasing this question but here is what I would like to find:
has_many :var, :through => :line

The above line of code is included in a model.  I want to return records that have a certain :var associated with it.  So if, for example, :var = 1234, I'd like to return all records that have that associated with it.

A: Assuming your main class is called Order, line is a belongs_to relation for Order, vars is a has_many relation for Line, and you are searching for the Var id
Order.joins(line: :vars).where('"vars"."id" = ?', 1234).uniq


A: Based on your comment i believe you need something like this
Order.where(:var => 1234)

or if you prefere plain sql
Order.where('var = 1234')

Activerecord has plenty of documentation on this, for example here 
