Module | Sequel::Model::Associations::ClassMethods |
In: |
lib/sequel/model/associations.rb
|
Each kind of association adds a number of instance 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 # or: many_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 without calling super, as they deal with callbacks and caching.
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.
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"}
association_reflections | [R] | All association reflections defined for this model (default: none). |
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. Also, in most cases you need to specify the plural association name when using one_to_many with the :one_to_one option.
The following options can be supplied:
# File lib/sequel/model/associations.rb, line 522 522: def associate(type, name, opts = {}, &block) 523: raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type] 524: raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name 525: 526: # merge early so we don't modify opts 527: orig_opts = opts.dup 528: orig_opts = association_reflection(opts[:clone])[:orig_opts].merge(orig_opts) if opts[:clone] 529: opts = orig_opts.merge(:type => type, :name => name, :cache => true, :model => self) 530: opts[:block] = block if block 531: opts = assoc_class.new.merge!(opts) 532: opts[:eager_block] = block unless opts.include?(:eager_block) 533: opts[:graph_join_type] ||= :left_outer 534: opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph) 535: conds = opts[:conditions] 536: opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds) 537: opts[:graph_conditions] = opts[:graph_conditions] ? opts[:graph_conditions].to_a : [] 538: opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select] 539: [:before_add, :before_remove, :after_add, :after_remove, :after_load, :extend].each do |cb_type| 540: opts[cb_type] = Array(opts[cb_type]) 541: end 542: 543: # find class 544: case opts[:class] 545: when String, Symbol 546: # Delete :class to allow late binding 547: opts[:class_name] ||= opts.delete(:class).to_s 548: when Class 549: opts[:class_name] ||= opts[:class].name 550: end 551: opts[:class_name] ||= ((self.name || '').split("::")[0..-2] + [camelize(opts.returns_array? ? singularize(name) : name)]).join('::') 552: 553: send("def_#{type}""def_#{type}", opts) 554: 555: orig_opts.delete(:clone) 556: orig_opts.merge!(:class_name=>opts[:class_name], :class=>opts[:class], :block=>block) 557: opts[:orig_opts] = orig_opts 558: # don't add to association_reflections until we are sure there are no errors 559: association_reflections[name] = opts 560: end
The association reflection hash for the association of the given name.
# File lib/sequel/model/associations.rb, line 563 563: def association_reflection(name) 564: association_reflections[name] 565: end
Modify and return eager loading dataset based on association options. Options:
# File lib/sequel/model/associations.rb, line 573 573: def eager_loading_dataset(opts, ds, select, associations) 574: ds = ds.select(*select) if select 575: if c = opts[:conditions] 576: ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c) 577: end 578: ds = ds.order(*opts[:order]) if opts[:order] 579: ds = ds.eager(opts[:eager]) if opts[:eager] 580: if opts[:eager_graph] 581: ds = ds.eager_graph(opts[:eager_graph]) 582: ds = ds.add_graph_aliases(opts.associated_key_alias=>[opts.associated_class.table_name, opts.associated_key_alias, SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column)]) if opts.eager_loading_use_associated_key? 583: else 584: ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias)) if opts.eager_loading_use_associated_key? 585: end 586: ds = ds.eager(associations) unless Array(associations).empty? 587: ds = opts[:eager_block].call(ds) if opts[:eager_block] 588: ds 589: end
Copy the association reflections to the subclass
# File lib/sequel/model/associations.rb, line 592 592: def inherited(subclass) 593: super 594: subclass.instance_variable_set(:@association_reflections, @association_reflections.dup) 595: end
Shortcut for adding a many_to_many association, see associate
# File lib/sequel/model/associations.rb, line 598 598: def many_to_many(*args, &block) 599: associate(:many_to_many, *args, &block) 600: end
Shortcut for adding a many_to_one association, see associate
# File lib/sequel/model/associations.rb, line 603 603: def many_to_one(*args, &block) 604: associate(:many_to_one, *args, &block) 605: end
Shortcut for adding a one_to_many association, see associate
# File lib/sequel/model/associations.rb, line 608 608: def one_to_many(*args, &block) 609: associate(:one_to_many, *args, &block) 610: end