Rails3 joins association
I have trouble using joins in rails3.
There are two models :
class User < ActiveRecord::Base
end
class Post < ActiveRecord::Base
belongs_to :master,class_name: 'User'
end
when i'm trying to find some posts like this:
Post.joins(:master).where("master.name like '%mike%'")
It goes wrong, logs show as following:
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column
'master.name' in 'where clause': SELECT `posts`.* FROM `posts` INNER JOIN
`users` ON `users`.`id` = `posts`.`master_id` WHERE (master.name like
'%mike%')
i think maybe i'd give an alias name for users, but how?
Any body kind to help me solving this problem?
No comments:
Post a Comment