class CommonEvent < ActiveRecord::Base
has_many :CommonEventsFollows
#增加消息数
def add_msg_count(count)
self.messge_count+=count
end
end
class CommonEventsFollow < ActiveRecord::Base
belongs_to :CommonEvent, :foreign_key => "event_id"
end
然后查询:
@events = CommonEvent.joins(:CommonEventsFollows).select('id,title,message_count').order('id desc').page(params[:page]).per(30)
生成的语句
Mysql2::Error: Unknown column 'common_events_follows.common_event_id' in 'on clause': SELECT id,title,message_count FROM `common_events` INNER JOIN `common_events_follows` ON `common_events_follows`.`common_event_id` = `common_events`.`id` ORDER BY id desc LIMIT 30 OFFSET 0
common_event_id 不是已经制定成 :foreign_key => "event_id"了吗??