Module: Torque::PostgreSQL::Base::ClassMethods
- Defined in:
- lib/torque/postgresql/base.rb
Instance Method Summary collapse
-
#belongs_to_many(name, scope = nil, **options, &extension) ⇒ Object
Specifies a one-to-many association.
-
#inherited(subclass) ⇒ Object
Whenever the base model is inherited, add a list of auxiliary statements like the one that loads inherited records’ relname.
-
#reset_table_name ⇒ Object
Make sure that table name is an instance of TableName class.
- #upsert_all(attributes, **xargs) ⇒ Object
Instance Method Details
#belongs_to_many(name, scope = nil, **options, &extension) ⇒ Object
Specifies a one-to-many association. The following methods for retrieval and query of collections of associated objects will be added:
collection
is a placeholder for the symbol passed as the name
argument, so belongs_to_many :tags
would add among others tags.empty?
.
- collection
-
Returns a Relation of all the associated objects. An empty Relation is returned if none are found.
- collection<<(object, …)
-
Adds one or more objects to the collection by adding their ids to the array of ids on the parent object. Note that this operation instantly fires update SQL without waiting for the save or update call on the parent object, unless the parent object is a new record. This will also run validations and callbacks of associated object(s).
- collection.delete(object, …)
-
Removes one or more objects from the collection by removing their ids from the list on the parent object. Objects will be in addition destroyed if they’re associated with
dependent: :destroy
, and deleted if they’re associated withdependent: :delete_all
. - collection.destroy(object, …)
-
Removes one or more objects from the collection by running
destroy
on each record, regardless of any dependent option, ensuring callbacks are run. They will also be removed from the list on the parent object. - collection=objects
-
Replaces the collections content by deleting and adding objects as appropriate.
- collection_singular_ids
-
Returns an array of the associated objects’ ids
- collection_singular_ids=ids
-
Replace the collection with the objects identified by the primary keys in
ids
. This method loads the models and callscollection=
. See above. - collection.clear
-
Removes every object from the collection. This destroys the associated objects if they are associated with
dependent: :destroy
, deletes them directly from the database ifdependent: :delete_all
, otherwise just remove them from the list on the parent object. - collection.empty?
-
Returns
true
if there are no associated objects. - collection.size
-
Returns the number of associated objects.
- collection.find(…)
-
Finds an associated object according to the same rules as ActiveRecord::FinderMethods#find.
- collection.exists?(…)
-
Checks whether an associated object with the given conditions exists. Uses the same rules as ActiveRecord::FinderMethods#exists?.
- collection.build(attributes = {}, …)
-
Returns one or more new objects of the collection type that have been instantiated with
attributes
and linked to this object by adding itsid
to the list after saving. - collection.create(attributes = {})
-
Returns a new object of the collection type that has been instantiated with
attributes
, linked to this object by adding itsid
to the list after performing the save (if it passed the validation). - collection.create!(attributes = {})
-
Does the same as
collection.create
, but raises ActiveRecord::RecordInvalid if the record is invalid. - collection.reload
-
Returns a Relation of all of the associated objects, forcing a database read. An empty Relation is returned if none are found.
Example
A Video
class declares belongs_to_many :tags
, which will add:
-
Video#tags
(similar toTag.where([id] && tag_ids)
) -
Video#tags<<
-
Video#tags.delete
-
Video#tags.destroy
-
Video#tags=
-
Video#tag_ids
-
Video#tag_ids=
-
Video#tags.clear
-
Video#tags.empty?
-
Video#tags.size
-
Video#tags.find
-
Video#tags.exists?(name: 'ACME')
-
Video#tags.build
-
Video#tags.create
-
Video#tags.create!
-
Video#tags.reload
The declaration can also include an options
hash to specialize the behavior of the association.
Options
- :class_name
-
Specify the class name of the association. Use it only if that name can’t be inferred from the association name. So
belongs_to_many :tags
will by default be linked to theTag
class, but if the real class name isSpecialTag
, you’ll have to specify it with this option. - :foreign_key
-
Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and “_ids” suffixed. So a Video class that makes a #belongs_to_many association with Tag will use “tag_ids” as the default
:foreign_key
.It is a good idea to set the
:inverse_of
option as well. - :primary_key
-
Specify the name of the column to use as the primary key for the association. By default this is
id
. - :dependent
-
Controls what happens to the associated objects when their owner is destroyed. Note that these are implemented as callbacks, and Rails executes callbacks in order. Therefore, other similar callbacks may affect the
:dependent
behavior, and the:dependent
behavior may affect other callbacks. - :touch
-
If true, the associated objects will be touched (the updated_at/on attributes set to current time) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute. Please note that with touching no validation is performed and only the
after_touch
,after_commit
andafter_rollback
callbacks are executed. - :optional
-
When set to
true
, the association will not have its presence validated. - :required
-
When set to
true
, the association will also have its presence validated. This will validate the association itself, not the id. You can use:inverse_of
to avoid an extra query during validation. NOTE:required
is set tofalse
by default and is deprecated. If you want to have association presence validated, userequired: true
. - :default
-
Provide a callable (i.e. proc or lambda) to specify that the association should be initialized with a particular record before validation.
- :inverse_of
-
Specifies the name of the #has_many association on the associated object that is the inverse of this #belongs_to_many association. See ActiveRecord::Associations::ClassMethods’s overview on Bi-directional associations for more detail.
Option examples:
belongs_to_many :tags, dependent: :nullify
belongs_to_many :tags, required: true, touch: true
belongs_to_many :tags, default: -> { Tag.default }
197 198 199 200 201 |
# File 'lib/torque/postgresql/base.rb', line 197 def belongs_to_many(name, scope = nil, **, &extension) klass = Associations::Builder::BelongsToMany reflection = klass.build(self, name, scope, , &extension) ::ActiveRecord::Reflection.add_reflection(self, name, reflection) end |
#inherited(subclass) ⇒ Object
Whenever the base model is inherited, add a list of auxiliary statements like the one that loads inherited records’ relname
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/torque/postgresql/base.rb', line 29 def inherited(subclass) super subclass.class_attribute(:auxiliary_statements_list) subclass.auxiliary_statements_list = {} record_class = ActiveRecord::Relation._record_class_attribute # Define the dynamic attribute that returns the same information as # the one provided by the auxiliary statement subclass.dynamic_attribute(record_class) do klass = self.class next klass.table_name unless klass.physically_inheritances? query = klass.unscoped.where(subclass.primary_key => id) query.pluck(klass.arel_table['tableoid'].cast('regclass')).first end end |
#reset_table_name ⇒ Object
Make sure that table name is an instance of TableName class
23 24 25 |
# File 'lib/torque/postgresql/base.rb', line 23 def reset_table_name self.table_name = TableName.new(self, super) end |
#upsert_all(attributes, **xargs) ⇒ Object
205 206 207 208 |
# File 'lib/torque/postgresql/base.rb', line 205 def upsert_all(attributes, **xargs) xargs = xargs.reverse_merge(on_duplicate: :update) ::ActiveRecord::InsertAll.new(self, attributes, **xargs).execute end |