简体   繁体   English

在 rails 固定装置中使用自定义外键标记引用

[英]Label references with custom foreign keys in rails fixtures

I have a lists class with a customized foreign_key我有一个带有自定义外键的列表

class List < ActiveRecord::Base
    belongs_to :user, foreign_key: 'author_id'

The table was created adding a reference and a foreign key as follows该表的创建添加了一个引用和一个外键,如下所示

add_reference :lists, :author, index: true
add_foreign_key :lists, :users, column: :author_id

Problem is, in my fixtures I'm trying to use label references but I keep getting a SQL exception问题是,在我的装置中,我试图使用标签引用,但我不断收到 SQL 异常
table lists has no column named author表列表没有名为 author 的列

lists.yml

list1:
    name: topchartsyea
    author: dude1

Any ideas?有任何想法吗? Is it even possible to use label references when using customized foreign keys like these?在使用像这样的自定义外键时,甚至可以使用标签引用吗?

Using rails 4.2.0使用导轨 4.2.0

I think your problem is that you're trying to use author when you've named the relationship user .我认为您的问题是您在将关系命名为user时尝试使用author If you want your fixture to work like that, then I think this should fix it up:如果你希望你的装置像那样工作,那么我认为这应该解决它:

class List < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM