Suppose I have two models that are similiar. One is called Primer3Template which holds a subset of the fields on Primer3Parameter.
I would like puts only the fields from Primer3Template which exist in Primer3Parameter, as well as upcase the field name and add some string interpolation. I have the code partially working, like this:
> t = Primer3Template.new
> @primer3_parameter = Primer3Parameter.where(:batch_detail_id => 7146)
> t.attributes.each do | name, value |
> puts "#{name.upcase}="
> end
So far so good, this outputs:
ID=
TYPE=
USER_ID=
BATCH_DETAIL_ID=
GENERATED=
STATUS=
P3_FILE_TYPE=
....etc
Next I am trying to output the value of the field from the @primer3_parameter variable, which has the same name as the current name being processed by the loop. I have tried this:
> t.attributes.each do | name, value |
> puts "#{name.upcase}=#{@primer3_parameter.name}"
> end
But this is outputting the name of the class, which is not what I want:
ID=Primer3Parameter
TYPE=Primer3Parameter
USER_ID=Primer3Parameter
BATCH_DETAIL_ID=Primer3Parameter
GENERATED=Primer3Parameter
I want it to skip some fields, and print the value of the field, something like this:
BATCH_DETAIL_ID=7146
GENERATED=true
STATUS=active
P3_FILE_TYPE=Type2
I also tried this:
> t.attributes.each do | name, value |
> x = "@primer3_parameters.first.#{name}"
> puts "#{name.upcase}=#{x}"
> end
Which gave me:
ID=@primer3_parameters.first.id
TYPE=@primer3_parameters.first.type
USER_ID=@primer3_parameters.first.user_id
BATCH_DETAIL_ID=@primer3_parameters.first.batch_detail_id
GENERATED=@primer3_parameters.first.generated