rails_has_elegance
rails_has_elegance

Reputation: 1692

Unexpected syntax error in block

I am basically copying one objects information to another. The code or approach in general might not be the most well-thought out, but that's not my problem right now.

This is the error:

syntax error, unexpected '=', expecting keyword_end
    original.send("#{attribute}") = edited.send("#{attribute}")
                                   ^

What I'm doing is looping through all the attributes of object2 and then "copying" each one to object 1. I could make this specific for each model, but I wanted to have one single implement_changes method, that would work for each class basically. The copy model belongs_to :edited and :original through polymorphic associations.

class Copy < ActiveRecord::Base
    def implement_changes
      original = self.original_type.constantize.find(original_id)
      edited = self.edited_type.constantize.find(edited_id)
      accessible_attributes = original_type.constantize.accessible_attributes.to_a.select{|a| a != "slug"}
      accessible_attributes.shift
      accessible_attributes.each do |attribute|
          original.send("#{attribute}") = edited.send("#{attribute}")
      end
      original.save!
    end

Why doesn't that block work?? I don't get it. Is the usage of send correct here? It wouldn't let me do original.attribute.

Any help appreciated! :)

Upvotes: 0

Views: 169

Answers (2)

BM5k
BM5k

Reputation: 1230

Is there a reason you're not using ActiveResource::Base#dup?

Upvotes: 1

John Ledbetter
John Ledbetter

Reputation: 14183

The method name for a setter includes the equals sign, and takes the new value as an argument. You might try:

original.send("#{attribute}=", edited.send(attribute))

Upvotes: 3

Related Questions