Module: Sequel::Model::Associations
- Included in:
- Sequel::Model
- Defined in:
- lib/sequel_model/associations.rb,
lib/sequel_model/association_reflection.rb
Overview
Associations are used in order to specify relationships between model classes that reflect relations between tables in the database using foreign keys.
Each kind of association adds a number of methods to the model class which are specialized according to the association type and optional parameters given in the definition. Example:
class Project < Sequel::Model
many_to_one :portfolio
one_to_many :milestones
end
The project class now has the following instance methods:
-
portfolio - Returns the associated portfolio
-
portfolio=(obj) - Sets the associated portfolio to the object, but the change is not persisted until you save the record.
-
milestones - Returns an array of associated milestones
-
milestones_dataset - Returns a dataset that would return the associated milestones, allowing for further filtering/limiting/etc.
-
add_milestone(obj) - Associates the passed milestone with this object
-
remove_milestone(obj) - Removes the association with the passed milestone
-
remove_all_milestones - Removes associations with all associated milestones
By default the classes for the associations are inferred from the association name, so for example the Project#portfolio will return an instance of Portfolio, and Project#milestones will return an array of Milestone instances, in similar fashion to how ActiveRecord infers class names.
Association definitions are also reflected by the class, e.g.:
Project.associations
=> [:portfolio, :milestones]
Project.association_reflection(:portfolio)
=> {:type => :many_to_one, :name => :portfolio, :class_name => "Portfolio"}
Associations can be defined by either using the associate method, or by calling one of the three methods: many_to_one, one_to_many, many_to_many. Sequel::Model also provides aliases for these methods that conform to ActiveRecord conventions: belongs_to, has_many, has_and_belongs_to_many. For example, the following three statements are equivalent:
associate :one_to_many, :attributes
one_to_many :attributes
has_many :attributes
Defined Under Namespace
Modules: EagerLoading Classes: AssociationReflection
Instance Method Summary collapse
-
#all_association_reflections ⇒ Object
Array of all association reflections for this model class.
-
#associate(type, name, opts = {}, &block) ⇒ Object
Associates a related model with the current model.
-
#association_reflection(name) ⇒ Object
The association reflection hash for the association of the given name.
-
#associations ⇒ Object
Array of association name symbols.
-
#many_to_many(*args, &block) ⇒ Object
(also: #has_and_belongs_to_many)
Shortcut for adding a many_to_many association, see associate.
-
#many_to_one(*args, &block) ⇒ Object
(also: #belongs_to)
Shortcut for adding a many_to_one association, see associate.
-
#one_to_many(*args, &block) ⇒ Object
(also: #has_many)
Shortcut for adding a one_to_many association, see associate.
Instance Method Details
#all_association_reflections ⇒ Object
Array of all association reflections for this model class
47 48 49 |
# File 'lib/sequel_model/associations.rb', line 47 def all_association_reflections association_reflections.values end |
#associate(type, name, opts = {}, &block) ⇒ Object
Associates a related model with the current model. The following types are supported:
-
:many_to_one - Foreign key in current model’s table points to associated model’s primary key. Each associated model object can be associated with more than one current model objects. Each current model object can be associated with only one associated model object. Similar to ActiveRecord/DataMapper’s belongs_to.
-
:one_to_many - Foreign key in associated model’s table points to this model’s primary key. Each current model object can be associated with more than one associated model objects. Each associated model object can be associated with only one current model object. Similar to ActiveRecord/DataMapper’s has_many.
-
:many_to_many - A join table is used that has a foreign key that points to this model’s primary key and a foreign key that points to the associated model’s primary key. Each current model object can be associated with many associated model objects, and each associated model object can be associated with many current model objects. Similar to ActiveRecord/DataMapper’s has_and_belongs_to_many.
The following options can be supplied:
-
*ALL types*:
-
:allow_eager - If set to false, you cannot load the association eagerly via eager or eager_graph
-
:class - The associated class or its name. If not given, uses the association’s name, which is camelized (and singularized unless the type is :many_to_one)
-
:eager - The associations to eagerly load when loading the associated object. For many_to_one associations, this is ignored unless this association is being eagerly loaded, as it doesn’t save queries unless multiple objects can be loaded at once.
-
:eager_block - If given, use the block instead of the default block when eagerly loading. To not use a block when eager loading (when one is used normally), set to nil.
-
:graph_conditions - The conditions to use on the SQL join when eagerly loading the association via eager_graph
-
:graph_join_type - The type of SQL join to use when eagerly loading the association via eager_graph
-
:order - the column(s) by which to order the association dataset. Can be a singular column or an array.
-
:reciprocal - the symbol name of the instance variable of the reciprocal association, if it exists. By default, sequel will try to determine it by looking at the associated model’s assocations for a association that matches the current association’s key(s). Set to nil to not use a reciprocal.
-
:select - the attributes to select. Defaults to the associated class’s table_name.*, which means it doesn’t include the attributes from the join table in a many_to_many association. If you want to include the join table attributes, you can use this option, but beware that the join table attributes can clash with attributes from the model table, so you should alias any attributes that have the same name in both the join table and the associated table.
-
-
:many_to_one:
-
:key - foreign_key in current model’s table that references associated model’s primary key, as a symbol. Defaults to :“#name_id”.
-
-
:one_to_many:
-
:key - foreign key in associated model’s table that references current model’s primary key, as a symbol. Defaults to :“#Sequel::Model::Associations.selfself.nameself.name.underscore_id”.
-
-
:many_to_many:
-
:join_table - name of table that includes the foreign keys to both the current model and the associated model, as a symbol. Defaults to the name of current model and name of associated model, pluralized, underscored, sorted, and joined with ‘_’.
-
:left_key - foreign key in join table that points to current model’s primary key, as a symbol. Defaults to :“#Sequel::Model::Associations.selfself.nameself.name.underscore_id”.
-
:right_key - foreign key in join table that points to associated model’s primary key, as a symbol. Defaults to Defaults to :“#Sequel::Model::Associations.namename.to_sname.to_s.singularize_id”.
-
:graph_join_table_conditions - The conditions to use on the SQL join for the join table when eagerly loading the association via eager_graph
-
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/sequel_model/associations.rb', line 119 def associate(type, name, opts = {}, &block) # check arguments raise ArgumentError unless [:many_to_one, :one_to_many, :many_to_many].include?(type) && Symbol === name # merge early so we don't modify opts opts = opts.merge(:type => type, :name => name, :block => block, :cache => true, :model => self) opts = AssociationReflection.new.merge!(opts) opts[:eager_block] = block unless opts.include?(:eager_block) opts[:graph_join_type] ||= :left_outer opts[:graph_conditions] = opts[:graph_conditions] ? opts[:graph_conditions].to_a : [] # find class case opts[:class] when String, Symbol # Delete :class to allow late binding opts[:class_name] ||= opts.delete(:class).to_s when Class opts[:class_name] ||= opts[:class].name end send(:"def_#{type}", name, opts) # don't add to association_reflections until we are sure there are no errors association_reflections[name] = opts end |
#association_reflection(name) ⇒ Object
The association reflection hash for the association of the given name.
146 147 148 |
# File 'lib/sequel_model/associations.rb', line 146 def association_reflection(name) association_reflections[name] end |
#associations ⇒ Object
Array of association name symbols
151 152 153 |
# File 'lib/sequel_model/associations.rb', line 151 def associations association_reflections.keys end |
#many_to_many(*args, &block) ⇒ Object Also known as: has_and_belongs_to_many
Shortcut for adding a many_to_many association, see associate
168 169 170 |
# File 'lib/sequel_model/associations.rb', line 168 def many_to_many(*args, &block) associate(:many_to_many, *args, &block) end |
#many_to_one(*args, &block) ⇒ Object Also known as: belongs_to
Shortcut for adding a many_to_one association, see associate
162 163 164 |
# File 'lib/sequel_model/associations.rb', line 162 def many_to_one(*args, &block) associate(:many_to_one, *args, &block) end |
#one_to_many(*args, &block) ⇒ Object Also known as: has_many
Shortcut for adding a one_to_many association, see associate
156 157 158 |
# File 'lib/sequel_model/associations.rb', line 156 def one_to_many(*args, &block) associate(:one_to_many, *args, &block) end |