开发者

Rails ActiveRecord self.___ vs @____

开发者 https://www.devze.com 2023-03-25 10:07 出处:网络
I think this this is a开发者_Python百科 pretty stupid question but I just confused myself I have several has_many defined. I can reference them in an instance method by saying self.------- If I try t

I think this this is a开发者_Python百科 pretty stupid question but I just confused myself

I have several has_many defined. I can reference them in an instance method by saying self.------- If I try to reference via @------ I get nil. Is it just that because it's an ActiveRecord object that it's not available as an instance variable inside the class?


They are not set as instance variables, they are methods. Same as for your properties, you should always access them like this:

self.property
self.read_attribute(:property)
self.write_attribute(:property, value)

They could even be instance variables at the end, but this is an implementation detail and you should always call the code over the well known interface, which is the methods.


associations are methods not instance variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消