我有一个ActiveRecord类,它的属性是一个数组(Postgres数组列),我希望数组中的项目是唯一的。覆盖阵列本身发生的方法的最好方法是什么?例如#< <?重写ActiveRecord属性的append方法(<<)
module PgTags
def tags=(value)
write_attribute :tags, value.uniq
end
end
class Rule < ActiveRecord::Base
include PgTags
end
r = Rule.new
r.tags = %w(one two one)
puts r.tags # outputs ['one', 'two']
r.tags << 'one'
puts r.tags # should still output ['one', 'two']
你使用的是什么版本的ActiveRecord? –