Module: ActiveRecord::Embedding::ClassMethods

Defined in:
lib/active_record/embedding.rb

Instance Method Summary collapse

Instance Method Details

#embeds(models) ⇒ Object

Embeds many ActiveRecord models which have been referenced with has_many.

Parameters:

  • models (Symbol)

    the name of the embedded models



69
70
71
# File 'lib/active_record/embedding.rb', line 69

def embeds(models)
  embed_attribute(models)
end

#embeds_many(models, options = { }) ⇒ Object

Embeds many ActiveRecord model

Parameters:

  • models (Symbol)

    the name of the embedded models

  • options (Hash) (defaults to: { })

    the embedding options



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_record/embedding.rb', line 40

def embeds_many(models, options = { })
  has_many models, options.merge(:dependent => :destroy, :autosave => true)
  embed_attribute(models)
  attr_accessible "#{models}_attributes".to_sym

  # What is marked for destruction does not evist anymore from
  # our point of view. FIXME: Really evil hack.
  alias_method "_super_#{models}".to_sym, models
  define_method models do
    # This is an evil hack. Because activerecord uses the items method itself to
    # find out which items are deleted, we need to act differently if called by
    # ActiveRecord. So we look at the paths in the Backtrace. If there is
    # activerecord-3 anywhere there, this is called by AR. This will work until
    # AR 4.0...
    if caller(0).select{|x| x =~ /activerecord-3/}.any?
      return send("_super_#{models}".to_sym) 
    end

    # Otherwise, when we are called by someone else, we will not return the items
    # marked for destruction.
    send("_super_#{models}".to_sym).reject(&:marked_for_destruction?)
  end
end