Viorel
(Viorel)
May 14, 2010, 6:24pm
1
I have something like this:
for xx in '01'..'09'
field_name='field'+xx
puts ModelName.field_name
end
My problem is that ModelName has no field named field_name, it have
fields like field01, field02,...,field09. So, how do I transform a
string in a field name?
It might be a trivial solution, but I can't find it. I would
appreciate any help.
What exactly are you trying to do with this?
Viorel
(Viorel)
May 14, 2010, 6:34pm
3
I am trying to access those fields
Viorel
(Viorel)
May 14, 2010, 6:35pm
4
What exactly are you trying to do with this?
I am trying to access these fields
puts ModelName.send(field_name)
Viorel
(Viorel)
May 14, 2010, 6:40pm
6
Thank you Michel! I works!
11155
(-- --)
May 14, 2010, 6:51pm
7
[Please quote when replying]
Viorel wrote:
Thank you Michel! I works!
Great. Now normalize your database schema to get rid of those repeating
fields.
Best,
David22
(David)
May 14, 2010, 7:10pm
8
Or model_name.read_attribute(field_name)
However if you multiple fields like that, it sounds like you need a
has_many relationship.
has_many :field_names
object.field_names.each do |field_mame|
# do something
end
Viorel
(Viorel)
May 14, 2010, 7:22pm
9
Or model_name.read_attribute(field_name)
However if you multiple fields like that, it sounds like you need a
has_many relationship.
has_many :field_names
object.field_names.each do |field_mame|
# do something
end
> [Please quote when replying]
> Viorel wrote:
> > Thank you Michel! I works!
> Great. Now normalize your database schema to get rid of those repeating
> fields.
You are absolutely right, both of you. It is not my schema, but I hope
that your answers will persuade those responsible. Than you!