DRY principle - how to implement?


#1

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:

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:

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
-> tools, scripts, tricks from our code lab: http://source.mihelac.org


#2

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


#3

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:

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:

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
-> tools, scripts, tricks from our code lab: 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.


#4

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

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
-> tools, scripts, tricks from our code lab: http://source.mihelac.org


#5

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
-> tools, scripts, tricks from our code lab: 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
-> tools, scripts, tricks from our code lab: http://source.mihelac.org


#6

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

regards

Justin


#7

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.


#8

Justin F. wrote:

Justin


Rails mailing list
removed_email_address@domain.invalid
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
-> tools, scripts, tricks from our code lab: http://source.mihelac.org


#9

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.