Module: ActiveHash::Associations::Methods
- Defined in:
- lib/associations/associations.rb
Instance Method Summary collapse
Instance Method Details
#belongs_to(association_id, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/associations/associations.rb', line 53 def belongs_to(association_id, = {}) = { :class_name => association_id.to_s.classify, :foreign_key => association_id.to_s.foreign_key }.merge() field [:foreign_key].to_sym define_method(association_id) do [:class_name].constantize.find_by_id(send([:foreign_key])) end define_method("#{association_id}=") do |new_value| attributes[[:foreign_key].to_sym] = new_value ? new_value.id : nil end end |
#has_many(association_id, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/associations/associations.rb', line 35 def has_many(association_id, = {}) define_method(association_id) do = { :class_name => association_id.to_s.classify, :foreign_key => self.class.to_s.foreign_key }.merge() klass = [:class_name].constantize if klass.respond_to?(:scoped) klass.scoped(:conditions => {[:foreign_key] => id}) else klass.send("find_all_by_#{options[:foreign_key]}", id) end end end |