Rails - Using join with custom-named associations
where
expects the actual table name, it just inserts it in SQL:
Article.where(whatever: {you: 'want'}).to_sql=> "SELECT `articles`.* FROM `articles` WHERE `whatever`.`you` = 'want'"
So you may use:
Measurement.joins(:examination).where(test_structures: { year: 2016, month: 5 })
But it's not good
Then you depend on table name while Model should abstract such things. You could use merge
:
Measurement.joins(:examination).merge(TestStructure.where(year: 2016, month: 5))
For joins you use the association name, but for where you need to use the table name
Measurement.joins(:examination).where(test_structures: { year: 2016, month: 5 })
or
Measurement.joins(:examination).where('test_structures.year': 2016, 'test_structures.month': 5 )
In this example table name examinations
should be provided instead of an association name examination
in the where
method.
Measurement.jons(:examination).where(examinations: { year: 2016, month: 5 })