# ActiveRecord method creation issue

**URL:** <https://www.ruby-forum.com/t/activerecord-method-creation-issue/182430>\
**Category:** Rails\
**Created:** [January 27, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/activerecord-method-creation-issue/182430 "2010-01-27T22:44:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![barorg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@barorg](https://www.ruby-forum.com/u/barorg)\
**Post date:** [January 27, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/activerecord-method-creation-issue/182430/1 "2010-01-27T22:44:09Z")

</div>

Sorry in advance if this is on the wrong forum/topic.

I’m a novice ruby developer, starting to use ActiveRecord, and am  
getting stuck with the following problem:

I have a class defined:

class User \< ActiveRecord::Base  
set\_table\_name “users”  
has\_one :locations  
has\_one :sessions  
has\_many :task\_postings  
has\_many :task\_descriptions

def get\_messages(uid)  
user = User.find(:first, :conditions =\> [“id = ‘#{uid}’”])  
if(user) then  
return user.messages\_waiting  
end  
return nil  
end  
end

In order to call the method, I have to create an instance of the class  
and refer to the method:

new\_user = User.new  
new\_user.get\_messages(1)

If I call the method by referring to the class, User.get\_messages, I get  
a non-existent method error. What I wanted is to be able to call the  
class as follows:

User.get\_messages(1)

in order to do that, I re-defined the class as:

def self.get\_messages(uid)

which works for the call, now I get an exception: “uninitialized  
constant MysqlCompat::MysqlRes”, which implies to me that the  
“singleton” version of the method call is not seeing the already open  
and functional ActiveRecord mysql class.

Can anyone shed some light on this?

Thanks VERY much,  
Gal Bar-or, Wyoming USA

---

<div class="post-metadata">

**Author:** ![barorg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@barorg](https://www.ruby-forum.com/u/barorg)\
**Post date:** [January 28, 2010, 12:09am UTC](https://www.ruby-forum.com/t/activerecord-method-creation-issue/182430/2 "2010-01-28T00:09:20Z")

</div>

Gal Bar-or wrote:

> Sorry in advance if this is on the wrong forum/topic.
> 
> I’m a novice ruby developer, starting to use ActiveRecord, and am  
> getting stuck with the following problem:
> 
> I have a class defined:
> 
> class User \< ActiveRecord::Base  
> set\_table\_name “users”  
> has\_one :locations  
> has\_one :sessions  
> has\_many :task\_postings  
> has\_many :task\_descriptions
> 
> def get\_messages(uid)  
> user = User.find(:first, :conditions =\> [“id = ‘#{uid}’”])  
> if(user) then  
> return user.messages\_waiting  
> end  
> return nil  
> end  
> end
> 
> In order to call the method, I have to create an instance of the class  
> and refer to the method:
> 
> new\_user = User.new  
> new\_user.get\_messages(1)
> 
> If I call the method by referring to the class, User.get\_messages, I get  
> a non-existent method error. What I wanted is to be able to call the  
> class as follows:
> 
> User.get\_messages(1)
> 
> in order to do that, I re-defined the class as:
> 
> def self.get\_messages(uid)

No, actually you don’t want to do this. get\_messages is conceptually a  
property of an individual user, not of the User class.

You also don’t need uid as an argument, since the User object you’re  
calling this on already knows its id. The proper way to do this would  
be

def get\_messages # note: no arguments  
self.messages\_waiting  
end

Finally, get\_\* is somewhat unidiomatic in Ruby; this method should  
perhaps just be called messages.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
