Re: cannot get property value from User class

7 views
Skip to first unread message

Abe Voelker

unread,
Jan 18, 2013, 3:35:22 PM1/18/13
to datam...@googlegroups.com
Give this a try:

get '/:nickname/feed' do
  user = User.first(:nickname => params[:nickname])
  if user
    @user_name = user.name
  else
    not_found 'unknown user'
  end
end

On Friday, January 18, 2013 12:55:25 PM UTC-6, Alex wrote:
Hi,
I have a problem getting the user’s name property. What am I doing wrong? I’m going insane why it’s not getting the name:

NoMethodError at /sican/feed
undefined method `name' for #<Enumerator: User:find("sican")>

Here’s the get method (commented out are some other alternatives that I’ve tried):

get '/:nickname/feed' do 
  @user_name  = User.find(params[:nickname]).name
  #user = User.get(params[:nickname])
  #@user_name = user.name
end

 Here’s the User class:

class User
  include DataMapper::Resource
  property :id,         Serial
  property :uid,        String
  property :name,       String
  property :nickname,   String
  property :created_on, DateTime
  has n, :posts
end

Any ideas? 

Alex

unread,
Jan 18, 2013, 3:42:54 PM1/18/13
to datam...@googlegroups.com
Thanks Abe, works great!

kristian

unread,
Jan 19, 2013, 12:03:25 AM1/19/13
to datam...@googlegroups.com
the get method needs the Serial or the Key (can be a list of args) as
you defined your key in the model.

in your case it is the id of the User.

- Krstian
Reply all
Reply to author
Forward
0 new messages