Module: Scrivito::Associations::ClassMethods
- Included in:
- BasicObj
- Defined in:
- app/cms/scrivito/associations.rb
Instance Method Summary collapse
-
#belongs_to(name, obj_class: name)
Specifies a one-to-one association with another class.
-
#belongs_to_many(name, obj_class: name)
Specifies a one-to-many association.
-
#has_many(name, obj_class: name, foreign_attribute: nil)
Specifies a one-to-many or many-to-many association with another class.
Instance Method Details
#belongs_to(name, obj_class: name)
This method returns an undefined value.
Specifies a one-to-one association with another class.
This method defines a foreign attribute for this class. If the other class contains the foreign attribute, then you should use has_many instead.
This method defines a reference
attribute with the given name
. Only instances of obj_class
are valid values for this attribute.
60 61 62 63 |
# File 'app/cms/scrivito/associations.rb', line 60 def belongs_to(name, obj_class: name) klass = to_single_class(obj_class) attribute(name, :reference, only: klass) end |
#belongs_to_many(name, obj_class: name)
This method returns an undefined value.
Specifies a one-to-many association.
This method defines a foreign attribute for this class. If the other class contains the foreign attribute, then you should use has_many instead.
This method defines a referencelist
attribute with the given name
. Only instances of obj_class
are valid values for this attribute.
106 107 108 109 |
# File 'app/cms/scrivito/associations.rb', line 106 def belongs_to_many(name, obj_class: name) klass = to_single_class(obj_class) attribute(name, :referencelist, only: klass) end |
#has_many(name, obj_class: name, foreign_attribute: nil)
This method returns an undefined value.
Specifies a one-to-many or many-to-many association with another class.
This method should only be used if the other class contains the foreign attribute. If the current class contains the foreign attribute, then you should use belongs_to or belongs_to_many instead.
This methods generates a new instance method <name>. This newly generated method searches for all obj_class
instances, where the foreign_attribute
references this instance. This newly generated method returns an Enumerable
of BasicObjs instances.
175 176 177 178 179 180 181 182 |
# File 'app/cms/scrivito/associations.rb', line 175 def has_many(name, obj_class: name, foreign_attribute: nil) klass = to_single_class(obj_class) attributes = to_attribute_names(foreign_attribute) define_method(name) do klass.where(attributes, :refers_to, self) end end |