Datamapper defining your own object methods, how?

So let's say I have a class as shown below

class List
  include DataMapper::Resource
  property :id, Serial  
  property :username, String

  def self.my_username
    return self[:username]
  end
end

list=List.create(:username=>,'jim')
list.my_username

When I run this, it tells me that the method cannot be found, and with a more detailed study, you can define class methods (not object methods) and that class methods do not have access to object data.

Is there a way to include these methods as object methods and access the object data? I am using Ruby 1.8.6 and the latest version of datamapper.

+3
source share
2 answers

Avoid all of yourself and become InstanceMethod:

class List
  include DataMapper::Resource
  property :id, Serial  
  property :username, String

  def my_username
    return self[:username]
  end
end

list=List.create(:username=>'jim')
list.my_username
+7
source

Model ( ) Resource ( ). , ( self.my_username), Model ( , ). , Resource ( ).

() . , 2.x named_scope. , , . DataMapper , . , DataMapper::Collection, all ( , +, -, & |.

+4

Source: https://habr.com/ru/post/1750475/


All Articles