当两个外键时Rails种子关联数据

时间:2015-08-22 22:20:02

标签: ruby-on-rails ruby ruby-on-rails-4

当您想要将数据播种到具有两列并且都引用相同模型的模型时,是否有人遇到过这种情况?

在每列引用唯一模型的模型中,我可以通过传入整数来为关系数据播种没问题。但是,当两列引用相同的模型时,我会不断收到以下错误。

我可以通过删除belongs_to文件中的2 match.rb关系(如下所示),播种数据,然后在种子完成后重新确定行来解决此问题。但这是一个绝对的麻烦,因为我在开发过程中重新调整了数据库。

我设置错了吗?谢谢你的期待。

  

错误消息

    rake aborted!
    ActiveRecord::AssociationTypeMismatch: Team(#33344260) expected, got Fixnum(#8019940)
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/associations/association.rb:216:in `raise_on_type_mismatch!'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/associations/belongs_to_association.rb:12:in `replace'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/associations/singular_association.rb:17:in `writer'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/associations/builder/association.rb:118:in `team1='
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:45:in `public_send'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:26:in `each'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/core.rb:455:in `init_attributes'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/core.rb:198:in `initialize'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:30:in `new'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:30:in `new'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/persistence.rb:33:in `create'
    /home/alzer/workspace/pawchallenge/db/seeds/09_matches.rb:1:in `<top (required)>'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
    /home/alzer/workspace/pawchallenge/db/seeds.rb:1:in `block in <top (required)>'
    /home/alzer/workspace/pawchallenge/db/seeds.rb:1:in `each'
    /home/alzer/workspace/pawchallenge/db/seeds.rb:1:in `<top (required)>'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
    /var/lib/gems/1.9.1/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
    /var/lib/gems/1.9.1/gems/railties-4.1.5/lib/rails/engine.rb:543:in `load_seed'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
    /var/lib/gems/1.9.1/gems/activerecord-4.1.5/lib/active_record/railties/databases.rake:132:in `block (2 levels) in <top (required)>'
  

模型/ match.rb

class Match < ActiveRecord::Base
  belongs_to :team1, class_name: 'Team', :foreign_key => 'team1'
  belongs_to :team2, class_name: 'Team', :foreign_key => 'team2'   
  default_scope { order(:id) } # Always orders the match objects by id when retrieved
end
  

模型/ team.rb

class Team < ActiveRecord::Base
  has_many :match
  default_scope { order(:world_ranking, :rank) } # Always orders the team objects by world ranking then rank when retrieved
end

1 个答案:

答案 0 :(得分:0)

是的,我把这个连接错了。在设置匹配模型时,我使用了列名team1&amp; team2将以下内容放入迁移文件t.integer :team1&amp; t.integer :team2

为了解决这个问题,我将其翻了回来,并将迁移文件更改为t.references :team1, index:true&amp; t.references :team2, index:true。然后,这会将相关列命名为team1_id&amp; team2_id

然后在模型中,我将行更改为belongs_to :team1, class_name: 'Team'belongs_to :team2, class_name: 'Team'

种子文件现在正确执行,代码全部有效。感谢您的帮助Ruby Racer