Question

Works

Retrieving user from database

def login(user_id)
  user = User.where(:user_id => user_id)
end

Does not work

Retrieve user group of user from database. I tried:

user_group = UserGroup.find(user.user_group.id)

CLASSES

class User
  include MongoMapper::Document
  plugin MongoMapper::Plugins::IdentityMap

  key :user_id,   String,   :required => true
  key :name,      String,   :required => true

  belongs_to :user_group
end

class UserGroup
  include MongoMapper::Document
  plugin MongoMapper::Plugins::IdentityMap

  key :name,   String,   :required => true

  many :users
end

ERROR

This is the error I get:

"NoMethodError - undefined method 'user_group' for #:"

Was it helpful?

Solution

I found the problem, ".where" returns an array and that was my mistake.

user = User.where(:user_id => user_id)

So instead of ".where", I used ".find_by_userid".

user = User.find_by_user_id(user_id)

This is Dynamic Querying, and it returns one entity which made it possible to do the following:

user_group = user.user_group
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top