0

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
1
  • 3
    It should be singular, not plural. has_one :performance_type You've also misspelt performance - might be just when typing your question though Commented Jun 9, 2016 at 16:13

3 Answers 3

4

has_one and belongs_to refer to a single object so you should use singularized form

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

class PerformanceType < ActiveRecord::Base
    belongs_to :performance
end
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure why this answer is being upvoted; the has_one associations are still plural, the "has_one :perfomace_type" is misspelled, and there is no mention of the migration files (so how do we know he actually setup the association the correct way?)
1

I think that this is what you want. The foreign key performance_type_id remains in performances. You have many performances with the same performance type, but each performance has only one performance type.

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

1 Comment

Doesn't work NoMethodError: undefined method `performance_type' for #<Performance:0x007fcf75a893b8>
0

You should change your Performance class listing the "has_one" relationship with the singular model (not plural):

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

class PerformanceType < ActiveRecord::Base
  belongs_to :performance
end

It's also important that your migration file is correctly configured, too:

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

8 Comments

Ok, now the associations are in singular form... The tables are or because I have correctly created the FK to the 3 tables. Now how access to performance_types column with ruby notation?
Ehm, doesnt work. How access to other columns by the association?
Can you post your migration file?
Whoa, OK. Looks like you've been doing a lot of migrations for this. It's a bit hard for me to follow. Could you post your schema.rb file?
@Tommyixi do your research, what you are saying is WRONG. has_one association are singular. downvoted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.