Call access methods from a Model object in Rails

I have the following method in my model

def reset_review_status
   needs_review = true
   save
end

There is an attribute in the model called needs_review, but when you debug it, it saves it as a new variable. If I do self.needs_review=true, it works fine. I have no attr_accessible clause, although I have one accepts_nested_attributes_for.

Any thoughts on why this might be happening?

+3
source share
1 answer

When you define an attribute in ActiveRecord, the following methods are available

# gets the value for needs_review
def needs_review
end

# sets the value for needs_review
def needs_review=(value)
end

You can invoke the installer using

needs_review = "hello"

, . , Ruby , .

def one
# variable needs_review created with value foo
needs_review = "foo"
needs_review
end

one
# => returns the value of the variable

def two
needs_review
end

two
# => returns the value of the method needs_review
# because no variable needs_review exists in the context
# of the method

:

  • self.method_name =, setter
  • self.method_name,
+4

Source: https://habr.com/ru/post/1791349/


All Articles