Module: Dynomite::Associations::ClassMethods
- Defined in:
- lib/dynomite/associations.rb
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ Object
Declare a
belongs_to
association for this document. -
#has_and_belongs_to_many(name, options = {}) ⇒ Object
Declare a
has_and_belongs_to_many
association for this document. -
#has_many(name, options = {}) ⇒ Object
Declare a
has_many
association for this document. -
#has_one(name, options = {}) ⇒ Object
Declare a
has_one
association for this document. -
#inherited(base) ⇒ Object
Create the association tracking attribute and initialize it to an empty hash.
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
Declare a belongs_to
association for this document.
class Post < ApplicationItem
belongs_to :categories
end
Association supports following operations:
-
create
-
create!
-
delete
When a name of an associated class doesn’t match an association name a class name should be specified explicitly either with class
or class_name
option:
belongs_to :item, class: Post
belongs_to :item, class_name: 'Post'
When associated class has own has_many
or has_one
association to the current class and the name doesn’t match a name of the current class this name can be specified with inverse_of
option:
class Category < ApplicationItem
has_many :items, class_name: 'Post'
end
class Post < ApplicationItem
belongs_to :categories, inverse_of: :items
end
By default a hash key attribute name is id
. If an associated class uses another name for a hash key attribute it should be specified in the belongs_to
association:
belongs_to :categories, foreign_key: :uuid
156 157 158 |
# File 'lib/dynomite/associations.rb', line 156 def belongs_to(name, = {}) association(:belongs_to, name, ) end |
#has_and_belongs_to_many(name, options = {}) ⇒ Object
Declare a has_and_belongs_to_many
association for this document.
class Post < ApplicationItem
has_and_belongs_to_many :tags
end
Association is an enumerable collection and supports following addition operations:
-
create
-
create!
-
destroy_all
-
delete_all
-
delete
-
<<
-
where
-
all
-
empty?
-
size
When a name of an associated class doesn’t match an association name a class name should be specified explicitly either with class
or class_name
option:
has_and_belongs_to_many :labels, class: Tag
has_and_belongs_to_many :labels, class_name: 'Tag'
When associated class has own has_and_belongs_to_many
association to the current class and the name doesn’t match a name of the current class this name can be specified with inverse_of
option:
class Tag < ApplicationItem
has_and_belongs_to_many :items, class_name: 'Post'
end
class Post < ApplicationItem
has_and_belongs_to_many :tags, inverse_of: :items
end
206 207 208 |
# File 'lib/dynomite/associations.rb', line 206 def has_and_belongs_to_many(name, = {}) association(:has_and_belongs_to_many, name, ) end |
#has_many(name, options = {}) ⇒ Object
Declare a has_many
association for this document.
class Category < ApplicationItem
has_many :posts
end
Association is an enumerable collection and supports following addition operations:
-
create
-
create!
-
destroy_all
-
delete_all
-
delete
-
<<
-
where
-
all
-
empty?
-
size
When a name of an associated class doesn’t match an association name a class name should be specified explicitly either with class
or class_name
option:
has_many :labels, class: Tag
has_many :labels, class_name: 'Tag'
When associated class has own belongs_to
association to the current class and the name doesn’t match a name of the current class this name can be specified with inverse_of
option:
class Post < ApplicationItem
belongs_to :item, class_name: 'Tag'
end
class Tag < ApplicationItem
has_many :posts, inverse_of: :item
end
65 66 67 |
# File 'lib/dynomite/associations.rb', line 65 def has_many(name, = {}) association(:has_many, name, ) end |
#has_one(name, options = {}) ⇒ Object
Declare a has_one
association for this document.
class Image < ApplicationItem
has_one :post
end
Association supports following operations:
-
create
-
create!
-
delete
When a name of an associated class doesn’t match an association name a class name should be specified explicitly either with class
or class_name
option:
has_one :item, class: Post
has_one :item, class_name: 'Post'
When associated class has own belong_to
association to the current class and the name doesn’t match a name of the current class this name can be specified with inverse_of
option:
class Post < ApplicationItem
belongs_to :logo, class_name: 'Image'
end
class Image < ApplicationItem
has_one :post, inverse_of: :logo
end
107 108 109 |
# File 'lib/dynomite/associations.rb', line 107 def has_one(name, = {}) association(:has_one, name, ) end |
#inherited(base) ⇒ Object
Create the association tracking attribute and initialize it to an empty hash.
14 15 16 17 |
# File 'lib/dynomite/associations.rb', line 14 def inherited(base) base.class_attribute :associations, instance_accessor: false base.associations = {} end |