如何让这种一对一的关联发挥作用?

发布于 2024-12-09 13:30:15 字数 1511 浏览 0 评论 0原文

我已经在管理模型和报告模型之间创建了一对一关联,但它尚未运行。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2024-12-16 13:30:15

您需要报告中的admin_id。您可以删除模型中的 foreign_key 内容。这是自动完成的。创建迁移,添加

add_column :reports, :admin_id, :integer

Run rake db:migrate 即可完成。

You need admin_id in reports. You can remove the foreign_key stuff in your models. That's done automatically. Create a migration, add

add_column :reports, :admin_id, :integer

Run rake db:migrate and you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文