【发布时间】:2011-08-23 17:28:11
【问题描述】:
我有两个模型,用户和事件。每个事件都有一个独特的管理员,他是用户角色。
当 Im trying to access @event.admin i get my admin user but if Im 尝试访问 @user.administered_events 以获取事件时,将显示此消息。
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'events.user_id' in 'where clause': SELECT events.* FROM events WHERE (events.user_id = 5)
Rails 正在尝试从 Event 而不是 admin_id 访问 user_id。我做错了什么?
class User < ActiveRecord::Base
has_many :administered_events, :class_name => "Event"
end
class Event < ActiveRecord::Base
belongs_to :admin, :class_name => "User"
end
class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.string :title
t.date :date
t.string :status
t.integer :admin_id
t.timestamps
end
end
def self.down
drop_table :events
end
end
【问题讨论】:
标签: ruby-on-rails activerecord associations one-to-one