Ruby noobie here.
When seeding my database, the association of Haiku -> belongs_to -> User is not persisting correctly via Mongoid. Haiku does not correctly store user_id in Mongo.
Doesn’t work
#seeds.rb
1.upto(100) do
user = User.create! name: 'foo'
haiku = Haiku.create! content: 'hello world', user: user
user.add_point({point_type: :tweet, value: 1, haiku: haiku})
end
Does Work
1.upto(100) do
user = User.create! name: 'foo'
haiku = Haiku.new content: 'hello world'
haiku.user = user
haiku.save!
user.add_point({point_type: :tweet, value: 1, haiku: haiku})
end
After digging through the source, I can only assume process_attributes is not setting the relation correctly.
Why does it not work in the before section but does in the after secion?
The mass assignment is trying to set the user and that should work.
It’s not working.
The reason is because
attr_accessibleis not called for the user reference. This macro needs to be for fields and references alike.