gpt4 book ai didi

ruby-on-rails - rails : One-to-many foreign key incorrect

转载 作者:行者123 更新时间:2023-12-04 06:31:29 38 4
gpt4 key购买 nike

我对 Rails 比较陌生。我正在尝试在 Rails 中设置一对多关联。但是,我认为我的 foreign_key 做错了,因为我的测试失败了。我的测试如下:

在用户规范中:

  it {should have_many :invitations}

用户模型:

  has_many :invitations

邀请模型:

belongs_to :sender, :class_name => "User"

邀请迁移:

class CreateInvitations < ActiveRecord::Migration
def change
create_table :invitations do |t|
t.integer :sender_id
t.string :token

t.timestamps
end
end
end

我从测试中得到的错误是:

Failure/Error: it {should have_many :invitations}
Expected User to have a has_many association called invitations (Invitation does not have a user_id foreign key.)

我不确定我哪里出错了。有什么想法吗?

最佳答案

错误说明问题不在belongs_to,而是在has_many

 has_many :invitations , :foreign_key => "sender_id"

关于ruby-on-rails - rails : One-to-many foreign key incorrect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271750/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com