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

Answers


Change it to

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

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)

Need Your Help

jquery table sorting and css fix for IE 8

jquery css internet-explorer table sorting

I'm attempting to make this page compatible with IE 8. The jquery right now allows for classes to be assigned on odds and evens, but doesn't seem to be working on IE 8.

Is there any way to identify a person's title through NLTK?

python algorithm machine-learning nltk

I'd like to be able to extract the title or job position of a person from a short description.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.