Module | Sequel::Model::Associations |
In: |
lib/sequel_model/association_reflection.rb
lib/sequel_model/associations.rb |
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:
If you want to override the behavior of the add_/remove_/remove_all_ methods, there are private instance methods created that a prepended with an underscore (e.g. _add_milestone). The private instance methods can be easily overridden, but you shouldn‘t override the public instance methods, as they deal with how associations are cached.
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
ASSOCIATION_TYPES | = | {} | Map of association type symbols to association reflection classes. |
Associates a related model with the current model. The following types are supported:
A one to one relationship can be set up with a many_to_one association on the table with the foreign key, and a one_to_many association with the :one_to_one option specified on the table without the foreign key. The two associations will operate similarly, except that the many_to_one association setter doesn‘t update the database until you call save manually.
The following options can be supplied:
# File lib/sequel_model/associations.rb, line 201 201: def associate(type, name, opts = {}, &block) 202: raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type] 203: raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name 204: 205: # merge early so we don't modify opts 206: opts = opts.merge(:type => type, :name => name, :block => block, :cache => true, :model => self) 207: opts = assoc_class.new.merge!(opts) 208: opts[:eager_block] = block unless opts.include?(:eager_block) 209: opts[:graph_join_type] ||= :left_outer 210: opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph) 211: opts[:graph_conditions] = opts[:graph_conditions] ? opts[:graph_conditions].to_a : [] 212: opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select] 213: [:before_add, :before_remove, :after_add, :after_remove, :after_load, :extend].each do |cb_type| 214: opts[cb_type] = Array(opts[cb_type]) 215: end 216: 217: # find class 218: case opts[:class] 219: when String, Symbol 220: # Delete :class to allow late binding 221: opts[:class_name] ||= opts.delete(:class).to_s 222: when Class 223: opts[:class_name] ||= opts[:class].name 224: end 225: 226: send("def_#{type}""def_#{type}", opts) 227: 228: # don't add to association_reflections until we are sure there are no errors 229: association_reflections[name] = opts 230: end
The association reflection hash for the association of the given name.
# File lib/sequel_model/associations.rb, line 233 233: def association_reflection(name) 234: association_reflections[name] 235: end
Shortcut for adding a many_to_many association, see associate
# File lib/sequel_model/associations.rb, line 255 255: def many_to_many(*args, &block) 256: associate(:many_to_many, *args, &block) 257: end
Shortcut for adding a many_to_one association, see associate
# File lib/sequel_model/associations.rb, line 249 249: def many_to_one(*args, &block) 250: associate(:many_to_one, *args, &block) 251: end
Shortcut for adding a one_to_many association, see associate
# File lib/sequel_model/associations.rb, line 243 243: def one_to_many(*args, &block) 244: associate(:one_to_many, *args, &block) 245: end