2016-06-09 86 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