簡體   English   中英

Rails ActiveRecord關聯問題

[英]Rails ActiveRecord Association issue

我有2個模型:

Performance和PerformanceType以及2個相應的表:Performance和Performance_types。

表演中有一個外鍵列“ 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_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

我認為這就是您想要的。 外鍵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

您應該更改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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM