Search code examples
ruby-on-railsrubyruby-on-rails-3.1ruby-on-rails-3.2associations

Ruby Has Many Association Error


Whenever I tried to run this rake command:

@user = User.find("1")
@contest = Contest.find("1")
@user.votes.create(:user => self, :contest => @contest)

I get this error:

User(#70104671283680) expected, got Object(#70104612331400)

This is my User.rb

has_many :votes
accepts_nested_attributes_for :votes
attr_accessible :votes_attributes

This is my Contest.rb

has_many :votes

And these are my migrations:

change_table :users do |t|
  t.references :votes
end

change_table :contests do |t|
  t.references :votes
  t.references :contest_items
end

change_table :votes do |t|
  t.belongs_to :users
  t.belongs_to :contests
end

Am I setting the votes incorrectly?


Solution

  • Replace self with a User, for example @user.