Reputation: 21553
Why does this work fine:
Tag.create(game_id: 1, tagged: u)
But this:
tags = Tag.where(game_id: 1, tagged: u).includes(:tagged)
gives error:
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'tags.tagged' in 'where clause': SELECT `tags`.* FROM `tags` WHERE `tags`.`game_id` = 1 AND `tags`.`tagged` = 1
BTW, u
is ActiveRecord::Base
sublcass.
Tag
table structure:
create_table :tags, force: true do |t|
t.references :game
t.references :tagged_by
t.references :tagged
t.timestamps
end
Upvotes: 0
Views: 689
Reputation: 346
The error occurs because the Tag
model does not have an association defined for tagged
.
To resolve this, you can either add a belongs_to :tagged
association in the Tag
model or explicitly reference the column name as the answer of @pungoyal:
tags = Tag.where(game_id: 1, tagged_id: u.id).includes(:tagged)
Upvotes: 0
Reputation: 1798
Try doing
tags = Tag.includes(:tagged).where(game_id: 1, tagged_id: u.id)
Upvotes: 2