有模型
class PlaylistModel < ActiveRecord::Base
/* code */
end
然后在 Controller Action 中有这样的代码
PlaylistController < ActionController::Base
def a_action
@item = Playlist.find(10)
@item[:visited] = true
end
end
但是 PlaylistModel
中(或播放列表表的架构中)没有定义 visited
属性!看起来一个新属性正在动态添加到对象中。这是正在发生的事情吗?所有这些功能在哪里定义/我在哪里可以阅读更多相关信息。
感谢您的帮助!
最佳答案
这是解释的属性 setter 快捷方式 here .它几乎等同于:
@item.attributes[:visited] = true
关于ruby-on-rails - 向 Rails 模型实例对象动态添加哈希属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12520095/