Robert Klemme
8/28/2007 5:30:00 PM
On 28.08.2007 13:03, Damjan Rems wrote:
> Thanks eval is good. What I have is database with fields
> name1,name2,name3,name4.. and
>
> name=eval("name#{num}")
I am not sure whether this is intentional but your variable "name" does
not contain the name but the name's value.
> works!
>
> Instead of something like
> name = case num
> when 1; name1
> when 2; name2
> .etc
Btw, you can also use Ruby's intelligent string "counting":
irb(main):001:0> n="name1"
=> "name1"
irb(main):002:0> n.succ
=> "name2"
irb(main):003:0> n.succ.succ
=> "name3"
irb(main):004:0> n.succ.succ.succ
=> "name4"
But I agree, this seems odd to have. You rather want a DB scheme where
you store all your values for nameX in another table together with an index:
owner_id (FK to your major table)
field_index (numeric)
value (whatever your type is)
You probably also want a uniqueness constraint on (owner_id, field_index).
Cheers
robert