Forum: Ruby on Rails DRY principle - how to implement?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bojan M. (Guest)
on 2006-03-23 16:30
(Received via mailing list)
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:

2)
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
Guest (Guest)
on 2006-03-23 16:43
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:
>
> 2)
> 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.
Nick S. (Guest)
on 2006-03-23 16:44
(Received via mailing list)
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
Bojan M. (Guest)
on 2006-03-23 17:40
(Received via mailing list)
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
Bojan M. (Guest)
on 2006-03-23 17:45
(Received via mailing list)
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
Justin F. (Guest)
on 2006-03-23 18:04
(Received via mailing list)
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+En...

regards

   Justin
Mike H. (Guest)
on 2006-03-23 18:29
(Received via mailing list)
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(c)
  SendStatus.find(:one, :condition => "code=#{c}")
end

or just as good

def self.code(c)
  find_by_code(c)
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(c)
  self.status = SendStatus.code(c)
end

and then you can write

record.set_status "sent"

you could also override the status= method, but this is probably far
enough.
Bojan M. (Guest)
on 2006-03-23 18:46
(Received via mailing list)
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
Eden B. (Guest)
on 2006-04-07 03:41
(Received via mailing list)
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.
This topic is locked and can not be replied to.