我正在尝试做类似的事情:
account.users << User.new
但我需要用户成为帐户上的一种方法。所以我尝试过类似的事情:
def users<<(obj)
但我没有运气。这在 Ruby 中甚至可以做到吗?我会这么认为,因为 ActiveRecord 关系在 Rails 中似乎是这样工作的。
最佳答案
检查这个答案:Rails: Overriding ActiveRecord association method
[此代码完全来自其他答案,供以后搜索者使用]
has_many :tags, :through => :taggings, :order => :name do
def << (value)
"overriden" #your code here
end
end
关于ruby-on-rails - 有没有办法覆盖 Ruby 中的 << 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9536839/