I've 2 model:
Performance and PerformanceType and 2 respective tables: performances and performance_types.
Into performances there is a foreign key column "performance_type_id".
How I can get the name of performance_type_id from the other table using ActiveRecord Associations?
class Performance < ActiveRecord::Base
has_one :users
has_one :rates
has_one :performace_types
end
class PerformanceType < ActiveRecord::Base
belongs_to :performances
end
With above code I try to do into console:
myvar = Performance.first
myvar.performance_types
NoMethodError: undefined method `performance_types' for #<Performance:0x007f9694b3e700>
I know that the problem is for my bad interpretation of active record associations, can anyone help me?
regards.
migrations from creating to foreign keys adding...
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
You've also misspelt performance - might be just when typing your question though – j-dexx