# DRY principle - how to implement?

**URL:** <https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745>\
**Category:** Rails\
**Created:** [March 23, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745 "2006-03-23T15:30:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/1 "2006-03-23T15:30:25Z")

</div>

Hi all!

I have ‘send\_status’ table which looks something like these:

id code title  
1 sent Sent  
2 error Sending error  
3 success Success

Next I would like to associate some processed records with their  
‘send\_status’. Is it better to use:

1. 

record.status\_id = SendStatus.find(:one, :condition =\> “code=‘sent’”).id

to set status of record, or it would be better to set ‘code’ as foreign  
key instead of ‘id’. That way I could write:

1. 

record.status\_code = ‘sent’;

I am not sure which is better way to do this, especially according to  
DRY principle of Rails; in first case it seems that I have one more SQL  
statement in for every record processed. Second way does not seems  
preferred in Rails (or I just didn’t see it anywhere?)

Does anyone have any suggestion about these? Thanks in advance,  
Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/2 "2006-03-23T15:44:38Z")

</div>

Take a look at the acts\_as\_enum (think thats the name) plugin. It allows  
your models to acts more like enumerations, which is basically what you  
are  
looking for here. So instead, you would be able to do something like  
SentStatus.SENT, or at least something like that. If you dont go that  
route,  
just use the first option. I wouldn’t say you are violating DRY by doing  
it  
that way. One way to help this, would be to add a class method in  
SentStatus:

class SentStatus  
…

def self.find\_sent  
self.find\_by\_code ‘sent’  
end  
…  
end

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/3 "2006-03-23T15:43:22Z")

</div>

Bojan M. wrote:

> Hi all!
> 
> I have ‘send\_status’ table which looks something like these:
> 
> id code title  
> 1 sent Sent  
> 2 error Sending error  
> 3 success Success
> 
> Next I would like to associate some processed records with their  
> ‘send\_status’. Is it better to use:
> 
> 1. 
> 
> record.status\_id = SendStatus.find(:one, :condition =\> “code=‘sent’”).id
> 
> to set status of record, or it would be better to set ‘code’ as foreign  
> key instead of ‘id’. That way I could write:
> 
> 1. 
> 
> record.status\_code = ‘sent’;
> 
> I am not sure which is better way to do this, especially according to  
> DRY principle of Rails; in first case it seems that I have one more SQL  
> statement in for every record processed. Second way does not seems  
> preferred in Rails (or I just didn’t see it anywhere?)
> 
> Does anyone have any suggestion about these? Thanks in advance,  
> Bojan
> 
> –  
> Bojan M.  
> Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
> → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

A separate table might be an overkill. I would just define some  
constants in the model, like: STATUS\_SENT = 1, STATUS\_ERROR = 2,  
STATUS\_SUCCESS = 3. It’s more efficient also.

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/4 "2006-03-23T16:45:02Z")

</div>

Nick S. wrote:

> ```
> 1)
> statement in for every record processed. Second way does not seems
> -> tools, scripts, tricks from our code lab: http://source.mihelac.org
> 
> ```
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

thanks for advice. Didn’t found acts\_as\_enum plugin. Are you sure it’s  
right name?

Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/5 "2006-03-23T16:40:33Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > Next I would like to associate some processed records with their
> > 
> > Bojan M.  
> > Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
> > → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)
> 
> A separate table might be an overkill. I would just define some  
> constants in the model, like: STATUS\_SENT = 1, STATUS\_ERROR = 2,  
> STATUS\_SUCCESS = 3. It’s more efficient also.

thanks for answer, but I need to add description of status code and  
icon, so just constants would not sattisfy.

Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/6 "2006-03-23T17:04:37Z")

</div>

Bojan M. wrote:

> thanks for advice. Didn’t found acts\_as\_enum plugin. Are you sure it’s  
> right name?

Acts as enumerated:  
[http://wiki.rubyonrails.org/rails/pages/Acts+As+Enumerated+Plugin](http://wiki.rubyonrails.org/rails/pages/Acts+As+Enumerated+Plugin)

regards

Justin

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/7 "2006-03-23T17:29:08Z")

</div>

Bojan M. wrote:

> ‘send\_status’. Is it better to use:  
> I am not sure which is better way to do this, especially according to  
> DRY principle of Rails; in first case it seems that I have one more  
> SQL statement in for every record processed. Second way does not seems  
> preferred in Rails (or I just didn’t see it anywhere?)
> 
> Does anyone have any suggestion about these? Thanks in advance,  
> Bojan

in SendStatus.rb

def self.code©  
SendStatus.find(:one, :condition =\> “code=#{c}”)  
end

or just as good

def self.code©  
find\_by\_code©  
end

then you can just write

record.status\_id = SendStatus.code(“sent”).id

or better yet, you don’t need the id

record.status = SendStatus.code(“sent”)

you could do this, in record.rb

def set\_status©  
self.status = SendStatus.code©  
end

and then you can write

record.set\_status “sent”

you could also override the status= method, but this is probably far  
enough.

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [March 23, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/8 "2006-03-23T17:46:15Z")

</div>

Justin F. wrote:

> Justin
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

oh thanx, these seems right what I looked for!

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [April 7, 2006, 1:41am UTC](https://www.ruby-forum.com/t/dry-principle-how-to-implement/55745/9 "2006-04-07T01:41:19Z")

</div>

Could someone confirm if acts\_as\_enumerated is compatible with the  
current  
version of Rails? All the information I find online appears to be from  
shortly after it was released.
