port5432
port5432

Reputation: 6403

Update ActiveRecord with a send

I am iterating through a hash, and if the hash key matches the column name (as defined by attr_accessible), then I want to update that column.

def self.load_primer3_output(rawfile_hash)
  primer3_output = Primer3Output.new
  rawfile_hash.each do |key, value|
    if primer3_output.class.accessible_attributes.include?(key)
      primer3_output.send(key) = value
    end
  end
  primer3_output.save
end

I am getting a syntax error on the send:

** [out :: 192.241.193.126]     /home/assay/apps/assay/releases/20130823054701/app/workers/primer3_query.rb:96: syntax error, unexpected '=', expecting keyword_end
** [out :: 192.241.193.126] (
** [out :: 192.241.193.126] SyntaxError
** [out :: 192.241.193.126] )
** [out :: 192.241.193.126]
** [out :: 192.241.193.126] primer3_output.send(key) = value
** [out :: 192.241.193.126]
** [out :: 192.241.193.126] ^

EDIT

eval works, by the way. I would prefer to use send though.

if primer3_output.class.accessible_attributes.include?(key)
    #primer3_output.send(key) = value
    eval("primer3_output.#{key} = value")
end

Upvotes: 2

Views: 758

Answers (2)

akbarbin
akbarbin

Reputation: 5105

when using send method the key should be symbol and string.

change to this when using symbol.

primer3_output.send(key.to_sym) = value

change to this when using string.

primer3_output.send("#{key}=", value)

Upvotes: -2

Santhosh
Santhosh

Reputation: 29174

Change it to

primer3_output.send("#{key}=", value)

Upvotes: 8

Related Questions