Rails3: Getting "no attribute named..." in simple join
I'm trying join
in Rails 3 for the first time, and can't get the most basic thing to work, so must be missing something major. Given
class Member < ActiveRecord::Base
belongs_to :status
...
end
class Status ActiveRecord::Base
has_many :members
...
end
when I try to use a join, testing in the console an example adapted from the Rails Guides
time_range = (Time.now.midnight - 1.day)..Time.now.midnight
Status.joins(:members).where('member.created_at' => time_range)
I get an error message,
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'member.created_at' in
'where clause': SELECT `statuses`.* FROM `开发者_如何学Pythonstatuses` INNER JOIN `members` ON
`members`.`status_id` = `statuses`.`id` WHERE (`member`.`created_at` BETWEEN
'2011-03-26 23:00:00' AND '2011-03-27 23:00:00')
Both models do have created_at
columns and this works: Member.where(:created_at => time_range)
. I've tried several permutations and used different columns but get the same Unknown column
error every time I try the join.
It should be members.created_at :) When you explicitly use the db table name, remember that it's in plural by rails convention.
精彩评论