2016-06-09 106 views
0

我有2個模型:Rails ActiveRecord Association問題

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 
+3

它應該是單數,而不是複數。 'has_one:performance_type'你也拼錯表現 - 可能只是輸入你的問題時, –

回答

1

我認爲這是你想要的。外鍵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 
+0

不起作用 NoMethodError:未定義的方法'performance_type'for#<性能:0x007fcf75a893b8> – michael

+0

您的解決方案有效。其他錯誤。 – michael

3

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 
+1

不知道爲什麼這個答案是upvoted; has_one關聯仍然是複數形式,「has_one:perfomace_type」拼寫錯誤,並且沒有提及遷移文件(因此我們如何知道他實際上以正確的方式設置關聯?) – Tommyixi

0

你應該改變你的表演類上市與單數模型(不是複數)的「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 
+0

好吧,現在這些關聯是單數形式的表格... 這些表是或因爲我已正確創建FK到3個表。 現在如何使用ruby符號訪問performance_types列? – michael

+0

那麼,一切正常嗎? – Tommyixi

+0

Ehm,不工作。 如何通過關聯訪問其他列? – michael