加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:由于外键验证,一对多关联失败

发布时间:2020-12-17 03:46:54 所属栏目:百科 来源:网络整理
导读:我已在rails中设置了一对多关联,但由于外键未正确设置,我的测试仍然失败.我想知道是否有人有任何建议. 我有两个型号 – 罗塔和用户.我希望用户“创建”一个rota.用户可以创建多个rota. 测试失败 *在rota_spec中:* it {should belong_to :creator}Expected R
我已在rails中设置了一对多关联,但由于外键未正确设置,我的测试仍然失败.我想知道是否有人有任何建议.

我有两个型号 – 罗塔和用户.我希望用户“创建”一个rota.用户可以创建多个rota.

测试失败

*在rota_spec中:*

it {should belong_to :creator}
Expected Rota to have a belongs_to association called creator (Rota does not have a creator_id foreign key.)

*在user_spec中:*

it {should have_many :created_rotas}
Expected User to have a has_many association called created_rotas (Rota does not have a creator_id foreign key.)

Rota.rb

belongs_to :creator,:class_name => "User"

User.rb

has_many :created_rotas,:class_name => "Rota",:foreign_key => "creator_id"

移民

class AddCreatorToRotas < ActiveRecord::Migration
  def change
    add_column :rotas,:creator_id,:string
  end
end

解决方法

你必须跑

rake db:test:prepare

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读