Problem while upgrading from version beta 20 to 2.0.1

19 views
Skip to first unread message

ag

unread,
Apr 28, 2011, 9:53:59 AM4/28/11
to Mongoid
Hi,

I am currently having a problem when upgrading from version beta 20 to
2.0.1

class User
include Mongoid::Document
include Mongoid::Timestamps

embeds_many :my_relations,
:class_name => 'Relation'
end

class Relation
include Mongoid::Document
include Mongoid::Timestamps

# tried changing this to belongs_to
referenced_in :target,
:class_name => 'User'
embedded_in :owner,
:class_name => 'User',
:inverse_of => :my_relations
end

Some how this does not work even after changing the referenced_in
macro to 'belongs_to'.
In the relation model I am trying to keep track to which other user is
the current user referenced.

Every time I try to create a new relation I get an error
NoMethodError: undefined method `first' for #<Relation:0x7f325a0f3a78>

Any ideas as to what is going wrong..

regards,
a



Chen Jack S Y

unread,
Apr 30, 2011, 10:38:17 AM4/30/11
to mon...@googlegroups.com
Not sure how you tried to create the new relation. Add inverse_of to
User model may help:

embeds_many :my_relations,
:class_name => 'Relation',
:inverse_of => :user

ag

unread,
May 10, 2011, 4:36:30 AM5/10/11
to Mongoid
Hi Chen,

I am creating user in the following manner..
user.my_relations.create \
:target => target, ....(other
properties)
end

The schema is somewhat like this User has embedded document (array) of
relations, and each relation has attributes and reference to a target
which is another user in db. I think with the new release perhaps it
may not be possible to create a has_one , belongs_to relationship on
the same collection, which is currently the case in my scenario.

Chen Jack S Y

unread,
May 10, 2011, 10:37:04 PM5/10/11
to mon...@googlegroups.com
Have you tried to replacing has_on/belongs_to with referenced_in or
something else? Did that work for you?

If not, the problem could be an embedding collection is not allowed to
reference to other collections other than its parent.

Nick Hoffman

unread,
May 11, 2011, 1:35:05 PM5/11/11
to mon...@googlegroups.com
The problem seems to be with the "target" association in Relation. If I set a relation's "target", the error occur. However, if I set a relation's "target_id", the error doesn't occur. Until this bug is fixed, you can use that as a workaround. Eg:

u1 = User.first
u2 = User.new
u2.my_relations.create :target_id => u1.id
u2.save

Nick Hoffman

unread,
May 11, 2011, 1:36:18 PM5/11/11
to mon...@googlegroups.com
BTW, please open an issue on GitHub for this. Include the code sample that you provided in your first post, as well as the workaround that I mentioned in my previous post.

https://github.com/mongoid/mongoid/issues
Reply all
Reply to author
Forward
0 new messages