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.
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2006-03-23 15: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 Mihelac
Informatika Mihelac, Bojan Mihelac s.p.  |  www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org
41231db9f0620d2dea414da4465821af?d=identicon&s=25 Guest (Guest)
on 2006-03-23 15:43
Bojan Mihelac 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 Mihelac
> Informatika Mihelac, Bojan Mihelac 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.
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-03-23 15: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
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2006-03-23 16:40
(Received via mailing list)
hunt@mets.ee wrote:
>> Next I would like to associate some processed records with their
>>
>> Bojan Mihelac
>> Informatika Mihelac, Bojan Mihelac 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 Mihelac
Informatika Mihelac, Bojan Mihelac s.p.  |  www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2006-03-23 16:45
(Received via mailing list)
Nick Stuart 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
> Rails@lists.rubyonrails.org
> 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 Mihelac
Informatika Mihelac, Bojan Mihelac s.p.  |  www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-23 17:04
(Received via mailing list)
Bojan Mihelac 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
97cbca14d17274370cce501bbea7980a?d=identicon&s=25 Mike Harris (Guest)
on 2006-03-23 17:29
(Received via mailing list)
Bojan Mihelac 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.
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2006-03-23 17:46
(Received via mailing list)
Justin Forder wrote:
>   Justin
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>

oh thanx, these seems right what I looked for!

--
Bojan Mihelac
Informatika Mihelac, Bojan Mihelac s.p.  |  www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org
61b70c2f195b0e669a8e25000148d9dd?d=identicon&s=25 Eden Brandeis (Guest)
on 2006-04-07 01: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.