Module: Consort::ActiveRecord::Mongoid::ClassMethods

Defined in:
lib/consort/active_record/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#active_record_consorts_with_mongoid?Boolean

Allows easy validation of whether ActiveRecord to Mongoid bridge is loaded.

Returns:

  • (Boolean)

    true if bridge is loaded



64
65
66
# File 'lib/consort/active_record/mongoid.rb', line 64

def active_record_consorts_with_mongoid?
  true
end

#belongs_to_mongoid(klass) ⇒ Object

Defines a belongs_to relationship with a Mongoid object. An appropriate foreign key column (of type String) must exist on your model.

Examples:

Migration

class AddMongoidFKeyToNarwhals < ActiveRecord::Migration
  def change
    add_column :narwhals, :pod_id, :string
  end
end

Model

class Narwhal < ActiveRecord::Base
  belongs_to_mongoid :pod
end

Parameters:

  • klass (Symbol)


54
55
56
57
58
59
60
# File 'lib/consort/active_record/mongoid.rb', line 54

def belongs_to_mongoid(klass)
  class_eval "    def \#{klass}\n      \#{klass.to_s.classify}.where(id: \#{klass.to_s.foreign_key})\n    end\n  CODE\nend\n"

#has_many_mongoid(klass) ⇒ Object

Defines a has_many relationship with a Mongoid object.

Examples:

has_many_mongoid :unicorns

Parameters:

  • klass (Symbol)

Since:

  • 0.0.2



27
28
29
30
31
32
33
# File 'lib/consort/active_record/mongoid.rb', line 27

def has_many_mongoid(klass)
  class_eval "    def \#{klass}\n      \#{klass.to_s.classify}.where(\#{name.foreign_key}: id)\n    end\n  CODE\nend\n"

#has_many_mongoids(klass) ⇒ Object

Deprecated.

Use #has_many_mongoid instead. Will be removed in 1.0.0.



36
37
38
39
# File 'lib/consort/active_record/mongoid.rb', line 36

def has_many_mongoids(klass)
  ActiveSupport::Deprecation.warn 'Please use the singular has_many_mongoid instead.'
  has_many_mongoid(klass)
end

#has_one_mongoid(klass) ⇒ Object

Defines a has_one relationship with a Mongoid object.

Examples:

has_one_mongoid :dolphin

Parameters:

  • klass (Symbol)


14
15
16
17
18
19
20
# File 'lib/consort/active_record/mongoid.rb', line 14

def has_one_mongoid(klass)
  class_eval "    def \#{klass}\n      \#{klass.to_s.classify}.where(\#{name.foreign_key}: id)\n    end\n  CODE\nend\n"