如何让这种一对一的关联发挥作用?
我已经在管理模型和报告模型之间创建了一对一关联,但它尚未运行。我使用 Devise
通过管理模型登录,因此在控制器中我使用 current_admin 帮助程序。愚蠢的问题,但是我需要运行什么迁移才能使其正常工作?
错误
ActiveRecord::StatementInvalid (PGError: ERROR: column reports.admin_id does not exist
2011-10-14T09:16:57+00:00 app[web.1]: LINE 1: SELECT "reports".* FROM "reports" WHERE ("reports".admin_id = ...
报告模型
belongs_to :admin, :foreign_key => "admin_id"
管理模型
has_one :report, :foreign_key => "admin_id"
控制器
@report = current_admin.report
架构
create_table "reports", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
end
create_table "admins", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
I've created a one-to-one association between my Admin and Report models and it isn't working just yet. I'm using Devise
to log in via an Admin model so in the controller I'm using the current_admin helper. Silly question, but what migration do I need to run to get this working?
Error
ActiveRecord::StatementInvalid (PGError: ERROR: column reports.admin_id does not exist
2011-10-14T09:16:57+00:00 app[web.1]: LINE 1: SELECT "reports".* FROM "reports" WHERE ("reports".admin_id = ...
Report model
belongs_to :admin, :foreign_key => "admin_id"
Admin model
has_one :report, :foreign_key => "admin_id"
Controller
@report = current_admin.report
Schema
create_table "reports", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
end
create_table "admins", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
报告
中的admin_id
。您可以删除模型中的foreign_key
内容。这是自动完成的。创建迁移,添加Run
rake db:migrate
即可完成。You need
admin_id
inreports
. You can remove theforeign_key
stuff in your models. That's done automatically. Create a migration, addRun
rake db:migrate
and you're done.