# Advice on model design

**URL:** <https://www.ruby-forum.com/t/advice-on-model-design/151280>\
**Category:** Rails\
**Created:** [October 19, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/advice-on-model-design/151280 "2008-10-19T17:44:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [October 19, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/advice-on-model-design/151280/1 "2008-10-19T17:44:05Z")

</div>

I’ve got an application with 2 related models. I want to search for  
data based on conditions in both models. I want to return items from  
the first model (Contract) grouped by conditions in the second  
(Status)

# At the moment, I’m basically doing it as shown below. It seems really ugly. Any suggestions how to do it better? Thanks Nick

class Contract \< ActiveRecord::Base  
belongs\_to :project  
belongs\_to :status

def self.activity\_find(team, director)  
conditions = Array.new  
conditions \<\< “projects.team\_id = #{service\_team}” if  
service\_team != “0”  
conditions \<\< “projects.director\_id = #{bid\_director}” if  
bid\_director != “0”  
ces = Array.new  
for group in Status::GROUPS  
conditions \<\< “statuses.” + Status.group\_sql(group)  
status\_group = find(:all, :conditions =\> conditions.join(" AND  
"), :include =\> [:status])  
conditions.delete( conditions.last )  
ces \<\< {:data =\> status\_group, :name =\> group[:description]}  
end  
end  
end

class Status \< ActiveRecord::Base  
has\_many :contracts

GROUPS = [{:stage =\> 1, :description =\> “been entered as  
prospect”},  
{:stage =\> 2, :description =\> “proposal has been  
submitted”},  
{:stage =\> 3, :description =\> “been notified as won”},  
{:stage =\> 4, :description =\> “been contracted”}]

def self.group\_sql(group)  
case group[:stage]  
when 1 then ‘opportunity=1’  
when 2 then ‘confirmed=1’  
when 3 then ‘notified=1’  
when 4 then ‘contracted=1’  
end  
end

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [October 20, 2008, 11:09am UTC](https://www.ruby-forum.com/t/advice-on-model-design/151280/2 "2008-10-20T11:09:06Z")

</div>

Hi!  
At first read this article

> **[Finder Objects in Rails](https://spin.atomicobject.com/2008/06/16/finder-objects-in-rails/)**
>
> Finder logic very often gets complicated very quickly. The responsibility for finding a specific kind of entity should not be given to a class that already fills another responsibility.

About Status model, at my mind is unnecessary. You can create status  
field in Contracts table

About:  
GROUPS = [{:stage =\> 1, :description =\> “been entered as  
prospect”},  
{:stage =\> 2, :description =\> “proposal has been  
submitted”},  
{:stage =\> 3, :description =\> “been notified as won”},  
{:stage =\> 4, :description =\> “been contracted”}]

In our project, such things we move to presenters  
in Contract:  
compose\_of :status, :class\_name =\> ‘ContractStatus’, :mapping =\> %  
(status to\_i) do |value|  
ContractStatus.new(value)  
end  
scopes\_state :status, :with =\> ContractStatus::STATUSES (see  
[Life in the Fast Lane](http://giantrobots.thoughtbot.com/2008/10/14/life-in-the-fast-lane))

in presenter ContractStatus:

STATUSES = [:notified\_as\_won, :submitted, contracted]  
HUMANIZE\_STATUSES =  
{  
:notified\_as\_won =\> ‘been notified as won’,  
:submitted =\>‘proposal has been submitted’  
}

def initialize(value)  
@int\_status =  
case value  
when Symbol  
STATUSES.index(value)  
when Integer  
value  
else  
raise  
end  
end

#show in view  
def to\_s  
HUMANIZE\_STATUSES[self.to\_sym]  
end

def to\_sym  
STATUSES.at(self.to\_i)  
end

#save in db  
def to\_i  
STATUSES[@int\_status]  
end

Good luck!

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [October 31, 2008, 5:24am UTC](https://www.ruby-forum.com/t/advice-on-model-design/151280/3 "2008-10-31T05:24:07Z")

</div>

Faust, sorry for the slow reply but thanks for your help!  
Nick
