当在 Rails 中调用 lib 中的 setter 时
我有一个疑问
在我的 lib 文件夹中,我有一个 file1.rb,它在模块下保存了所有一些常用方法。
我有一个设置器,
def a=(ab)
self.name == ab if ab
end
我已通过
include file1
将这个 file1.rb 包含在我的模型中,我的疑问是如何这个设置器正在被调用,这会像我在模型中提到的那样被自动调用吗?
I am having a doubt
In my lib folder i am having a file1.rb which has all some common methods kept under a module..
I am having a setter
def a=(ab)
self.name == ab if ab
end
I have included this file1.rb in my model by
include file1
my doubt is how this setter is getting called will this be called automatically as i have mentioned in model ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将模块包含在模型上,可以从中访问它的所有方法。
这与直接在模型上定义方法相同。
因此,您应该能够从任何您想要的地方调用它。
By including the module on the model, all it's methods become accessible from it.
It would be the same as defining the method directly on your model.
So, you should be able to call it from wherever you want.