Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Virtual attributes can't update real attributes #5732

Closed
Vanuan opened this Issue · 2 comments

2 participants

@Vanuan

Suppose I have a model with both real and virtual attributes:

class Table < ActiveRecord::Base

  def my_real_attribute
    read_attribute(:my_real_attribute)
  end

  def my_real_attribute=(arg)
    write_attribute(:my_real_attribute, arg)
  end

  def my_attribute=(arg)
    my_real_attribute = arg
  end

  def my_attribute
    my_real_attribute
  end

end

The problem is that when I create a new instance:

table = Table.new('my_attribute' => 0)

my real attribute is not updated:

assert table.my_attribute == 0 # fails
@carlosantoniodasilva

Setting my_real_attribute = arg actually will set a local variable instead of calling the model method, you probably have to use self.my_real_attribute = arg to call the method.

@Vanuan Vanuan closed this
@Vanuan

oops, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.