has many - Querying for parent object in Mongoid has_many relationship -


i have 2 objects

class user   ...   has_many :roles end  class role   ...   belongs_to :user end 

i can assign roles users without problem. however, want able query users have particular role. should incredibly simple. however, seem incapable of getting results.

i think should work:

role = role.first u = user.first u.roles << role u.save user.in(role_ids: role._id) 

however, returns nothing. role assigned user, can't figure out how query users specific role. know has simple, driving myself crazy trying figure out.

nevermind. should've been using has_and_belongs_to_many instead of "has_many" , "belongs_to" since many users can share role. indeed simple solution, going wrong. once changed relationship has_and_belongs_to_many, can "role.users" list of users role.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -