【问题标题】:Rails ActiveRecord Association issueRails ActiveRecord 关联问题
【发布时间】:2016-06-09 16:02:56
【问题描述】:

我有 2 个模型:

Performance 和 PerformanceType 以及 2 个各自的表:performances 和 performance_types。

进入performances有一个外键列“performance_type_id”。

如何使用 ActiveRecord 关联从另一个表中获取 performance_type_id 的名称?

class Performance < ActiveRecord::Base
has_one :users
has_one :rates
has_one :performace_types
end

class PerformanceType < ActiveRecord::Base
    belongs_to :performances
end

使用上面的代码我尝试在控制台中做:

myvar = Performance.first
myvar.performance_types
NoMethodError: undefined method `performance_types' for #<Performance:0x007f9694b3e700>

我知道问题出在我对活动记录关联的错误解释上,谁能帮助我?

问候。

从创建到添加外键的迁移...

class CreatePerformances < ActiveRecord::Migration
  def change
    create_table :performances do |t|

      t.timestamps null: false
    end
  end
end

class CreatePerformanceTypes < ActiveRecord::Migration
  def change
    create_table :performance_types do |t|

      t.timestamps null: false
    end
  end
end

class AddPerformanceTypeIdToPerformance < ActiveRecord::Migration
  def change
    add_column :performances, :performance_type_id, :integer
  end
end

class AddAppointmentInfoToPerformance < ActiveRecord::Migration
  def change
    add_column :performances, :user_id, :integer
    add_column :performances, :start_at, :datetime
    add_column :performances, :end_at, :datetime
  end
end

class AddUserToPerformances < ActiveRecord::Migration
  def change
    add_foreign_key :performances, :users
  end
end

class AddTypeToPerformances < ActiveRecord::Migration
  def change
    add_foreign_key :performances, :performance_types
  end
end

class AddAdditionalFieldsToPerformanceType < ActiveRecord::Migration
  def change
    add_column :performance_types, :name, :string
    add_column :performance_types, :description, :string
  end
end

class AddPerformanceTypeToRate < ActiveRecord::Migration
  def change
    add_foreign_key :rates, :performance_types
  end
end

class AddRateToPerformances < ActiveRecord::Migration
  def change
    add_column :performances, :rate_id, :integer
    add_foreign_key :performances, :rates
  end
end

【问题讨论】:

  • 应该是单数,而不是复数。 has_one :performance_type 你也拼错了性能 - 可能只是在输入你的问题时

标签: ruby-on-rails ruby activerecord


【解决方案1】:

has_onebelongs_to 指的是单个对象,因此您应该使用单数形式

class Performance < ActiveRecord::Base
    has_one :users
    has_one :rates
    has_one :performance_type
end

class PerformanceType < ActiveRecord::Base
    belongs_to :performance
end

【讨论】:

  • 不知道为什么这个答案被赞成; has_one 关联仍然是复数,“has_one :perfomace_type”拼写错误,并且没有提及迁移文件(那么我们怎么知道他实际上以正确的方式设置了关联?)
【解决方案2】:

我认为这就是你想要的。外键 performance_type_id 保留在性能中。您有许多具有相同表演类型的表演,但每个表演只有一种表演类型。

class Performance < ActiveRecord::Base 
  # note performance type is singular
  belongs_to :performance_type
end

class PerformanceType < ActiveRecord::Base
  has_many :performances 
end

p = Performance.first
# this should work 
p.performance_type

【讨论】:

  • 不起作用 NoMethodError: undefined method `performance_type' for #<0x007fcf75a893b8>
【解决方案3】:

您应该更改 Performance 类,列出与单数模型(不是复数)的“has_one”关系:

class Performance < ActiveRecord::Base
  has_one :user
  has_one :rate
  has_one :performance_type
end

class PerformanceType < ActiveRecord::Base
  belongs_to :performance
end

正确配置迁移文件也很重要:

class CreatePerformances < ActiveRecord::Migration
  def change
    create_table :performances do |t|
    end

    create_table :performance_types do |t|
      t.belongs_to :performance, index: true
      t.belongs_to :rate
      t.belongs_to :user
    end
  end
end

【讨论】:

  • 好的,现在关联是单数形式...这些表是或者因为我已经正确地为 3 个表创建了 FK。现在如何使用 ruby​​ 表示法访问 performance_types 列?
  • 嗯,没用。如何通过关联访问其他列?
  • 你能发布你的迁移文件吗?
  • 哇,好的。看起来你已经为此做了很多迁移。我有点难以跟上。你能发布你的schema.rb 文件吗?
  • @Tommyixi 做你的研究,你说的是错误的。 has_one 关联是单数。被否决。
猜你喜欢
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
相关资源
最近更新 更多