# Calling a method on class creation

**URL:** <https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871>\
**Category:** Rails\
**Created:** [August 24, 2007, 1:16am UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871 "2007-08-24T01:16:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 1:16am UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/1 "2007-08-24T01:16:17Z")

</div>

I have a model User that has property called status. Status is just a  
string that can only be evaluated and not persisted. I.e. it doesn’t  
have a field in the user table.

Anyways, I would like a method get\_status to be called anytime I  
create a new instance of User. For example:

user=User.new

Since I created a new instance of User, I should now have access to  
the status property, e.g.:

user.status  
=\>“Ready”

Here is my User class:

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles

def evaluate\_status  
#evaluate the status  
#@status = …  
end  
end

How would I call get\_status whenever a new instance of User is  
created? It’s constructor?

Thanks

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 2:19am UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/2 "2007-08-24T02:19:23Z")

</div>

initialize or after\_initialize should do what you need. Initialize is  
called by ruby when the object is, well, initialized…

cheers,  
Dan

=================================  
class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles

def initialize  
evaluate\_status  
end

# def evaluate\_status #evaluate the status #@status = … end end

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 7:12am UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/3 "2007-08-24T07:12:43Z")

</div>

It doesn’t seem like the initializer gets called when you create a new  
instance using .find(), e.g.:

c=ContentCampaign.find(1)  
c.status

=\> nill

However, If I create a new instance using new it works, e.g:

d=ContentCampaign.new  
c.status  
=\> Waiting

Here is my class, what’s going on?

class ContentCampaign \< ActiveRecord::Base  
belongs\_to :category, :class\_name =\> “Category”, :foreign\_key =\>  
“category\_id”

def initialize  
@status= evaluate\_status  
end

def evaluate\_status  
‘Waiting’  
end

def status  
@status  
end

end

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/4 "2007-08-24T14:38:49Z")

</div>

eggie5 wrote:

> d=ContentCampaign.new  
> @status= evaluate\_status
> 
> end

Try instead:

class ContentCampaign \< ActiveRecord::Base  
belongs\_to :category, :class\_name =\> “Category”, :foreign\_key  
=\>“category\_id”

def evaluate\_status  
‘Waiting’  
end

def status  
@status ||= evaluate\_status  
end  
end

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/5 "2007-08-24T21:16:54Z")

</div>

On 8/24/07, eggie5 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> THANKS! that’s the trick I was looking for. Now this reminds me of an  
> example from the essential rails book…
> 
> So, it looks like status is called automatically when a new instance  
> of the class is created?

does defining an after\_initialize method not work for you?

by the way, your belongs\_to method doesn’t need the :class\_name or  
:foreign\_key specifications… These can be inferred from the  
association itself.

Adam

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/6 "2007-08-24T21:05:50Z")

</div>

THANKS! that’s the trick I was looking for. Now this reminds me of an  
example from the essential rails book…

So, it looks like status is called automatically when a new instance  
of the class is created?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 24, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/calling-a-method-on-class-creation/112871/7 "2007-08-24T21:59:03Z")

</div>

sorry I didn’t see you solution till after I tried accessor method.

Regarding the foreign\_key and class\_name things, rails was warning me  
that if I could, I should specify them to avoid confusion. Since it  
was not big deal I just put them in. It’s the least I could do for  
ActiveRecord!
