Module: BelongsToMany::ClassMethods
- Defined in:
- lib/vex/active_record/belongs_to_many.rb
Instance Method Summary collapse
-
#belongs_to_many(name, opts = {}) ⇒ Object
belongs_to_many :havings, :supersedes => lambda { |name| has_and_belongs_to_many name, :join_table => :belongings_havings, :class_name => “Having” }.
Instance Method Details
#belongs_to_many(name, opts = {}) ⇒ Object
belongs_to_many :havings,
:supersedes => lambda { |name|
has_and_belongs_to_many name, :join_table => :belongings_havings, :class_name => "Having"
}
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/vex/active_record/belongs_to_many.rb', line 54 def belongs_to_many(name, opts = {}) s = name.to_s.singularize ids_name = "#{s}_ids" if supersedes = opts.delete(:supersedes) # if the belongs_to_many column exists we instanciate the superseded # association with the name + "_superseded" and build the belongs_to_many # association; if not, we only build the old association with the # current name. # if column_names.include?(ids_name) supersedes.call("#{name}_superseded") belongs_to_many(name, opts) else supersedes.call(name) end return end # class_name = opts[:class_name] || s.camelize.constantize self.class_eval code = <<RUBY def #{name} #{class_name}.find(#{ids_name}).freeze end def #{name}=(array) self.#{ids_name} = array.collect(&:id).uniq save! unless new_record? end def #{ids_name} read_attribute(:#{ids_name}).to_s.split(/[^0-9]+/).reject(&:blank?).collect { |s| Integer(s) } end def #{ids_name}=(array) write_attribute(:#{ids_name}, array.empty? ? nil : "/" + array.join("/") + "/") end RUBY end |