ActiveRecord :: StatementInvalid未知列错误

时间:2013-05-08 19:26:33

标签: ruby-on-rails ruby activerecord

为什么这样可行:

Tag.create(game_id: 1, tagged: u)

但是这个:

tags = Tag.where(game_id: 1, tagged: u).includes(:tagged)

给出错误:

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,uActiveRecord::Base sublcass。

Tag表格结构:

create_table :tags, force: true do |t|
    t.references :game
    t.references :tagged_by
    t.references :tagged
    t.timestamps
end

1 个答案:

答案 0 :(得分:2)

尝试

tags = Tag.includes(:tagged).where(game_id: 1, tagged_id: u.id)