ActiveRecord::AssociationTypeMismatch

258 views
Skip to first unread message

McCauley

unread,
May 17, 2014, 8:35:47 PM5/17/14
to rubyonra...@googlegroups.com
Sorry for my english.

I have to create twitter like app. I created 3 models User, Tweet, Followership

class User < ActiveRecord::Base
  has_many
:tweets
  accepts_nested_attributes_for
:tweets
  has_many
:followerships, class_name: 'Followership', foreign_key: 'followed_id'
  has_many
:followerships, class_name: 'Followership', foreign_key: 'follower_id'
  has_many
:followed, through: :followedships, source: :followed
  has_many
:followers, through: :followerships, source: :follower
end
class Tweet < ActiveRecord::Base
  belongs_to
:user
end
class Followership < ActiveRecord::Base
  belongs_to
:follower, class_name: 'User'
  belongs_to
:followed, class_name: 'User'
end

I have yaml file with UTF-8 data to add to my db. I changed my seeds.rb file in such a way:
seed_file = File.join(Rails.root, 'db', 'seeds.yml')
config
= YAML::load_file(seed_file)
User.create(config["users"])
Followership.create(config["followerships"])

Then i tried to run rake db:seed and got 
alexkd@Active-pc:~/WebDev/rails/twitter$ rake db:seed
rake aborted
!
ActiveRecord::AssociationTypeMismatch: Tweet(#97039630) expected, got Hash(#81744670)
...

Whats the problem? I have no ideas. ruby 2.0, Rails 4.1.0, sQlite3


Matt Jones

unread,
May 18, 2014, 4:24:20 PM5/18/14
to rubyonra...@googlegroups.com
Hard to say for certain without a backtrace or the yaml, but my first guess would be that you're trying to assign to `tweets` instead of the `tweets_attributes` method that `accepts_nested_attributes` has generated.

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages