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
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.orgA 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. -- Posted via http://www.ruby-forum.com/.
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 On 3/23/06, Bojan Mihelac <lists@mihelac.org> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060323/7ae8c3f0/attachment.html
hunt@mets.ee wrote:> 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. >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
Nick Stuart wrote:> 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 > > On 3/23/06, *Bojan Mihelac* < lists@mihelac.org > <mailto:lists@mihelac.org>> 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 <http://www.informatikamihelac.com> > -> tools, scripts, tricks from our code lab: http://source.mihelac.org > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org> > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsthanks 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
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+Enumerated+Plugin regards Justin
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 > >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.
Justin Forder wrote:> 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+Enumerated+Plugin > > regards > > 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
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. On 3/23/06, Mike Harris <GENIE@prodigy.net> wrote:> > 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 > > > > > 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. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/6014502b/attachment-0001.html