# Constants in model

**URL:** <https://www.ruby-forum.com/t/constants-in-model/178802>\
**Category:** Rails\
**Created:** [November 11, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/constants-in-model/178802 "2009-11-11T14:48:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mehmet\_Davut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@Mehmet\_Davut](https://www.ruby-forum.com/u/Mehmet_Davut)\
**Post date:** [November 11, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/constants-in-model/178802/1 "2009-11-11T14:48:52Z")

</div>

Hi,

I have a constant in Model like;

class Post \< ActiveRecord::Base

STATES = {:id =\> 100, :status =\> “public”},  
{:id =\> 110, :status =\> “protected”},  
{:id =\> 120, :status =\> “private”},  
{:id =\> 130, :status =\> “deleted”}  
end

And i write a method to find elements in that STATES constant

class Post \< ActiveRecord::Base

…

def self.find\_state(key, value)  
Post::STATES.collect { |state| return state if state[:"#{key}"] ==  
value }  
end  
end

When I write Post.find\_state(‘status’, ‘private’) it brings me back  
that hash and i get id with the symbol of [:id].

How can I improve this? Could i use it like “Post::STATES.find(:id =\>  
120)” or something else? or Could i use it in a named\_scope like  
“named\_scope :status”?

I need your advices.

Thanks

–  
Mehmet Davut  
davit~  
www.mehmetdavut.com.tr  
0532 224 93 65
