Module | Sequel::Model::Associations::ClassMethods |
In: |
lib/sequel/model/associations.rb
lib/sequel/model/deprecated.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 486 486: def associate(type, name, opts = {}, &block) 487: raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type] 488: raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name 489: 490: # merge early so we don't modify opts 491: orig_opts = opts.dup 492: orig_opts = association_reflection(opts[:clone])[:orig_opts].merge(orig_opts) if opts[:clone] 493: opts = orig_opts.merge(:type => type, :name => name, :cache => true, :model => self) 494: opts[:block] = block if block 495: opts = assoc_class.new.merge!(opts) 496: opts[:eager_block] = block unless opts.include?(:eager_block) 497: opts[:graph_join_type] ||= :left_outer 498: opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph) 499: conds = opts[:conditions] 500: opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds) 501: opts[:graph_conditions] = opts[:graph_conditions] ? opts[:graph_conditions].to_a : [] 502: opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select] 503: [:before_add, :before_remove, :after_add, :after_remove, :after_load, :extend].each do |cb_type| 504: opts[cb_type] = Array(opts[cb_type]) 505: end 506: 507: # find class 508: case opts[:class] 509: when String, Symbol 510: # Delete :class to allow late binding 511: opts[:class_name] ||= opts.delete(:class).to_s 512: when Class 513: opts[:class_name] ||= opts[:class].name 514: end 515: 516: send("def_#{type}""def_#{type}", opts) 517: 518: orig_opts.delete(:clone) 519: orig_opts.merge!(:class_name=>opts[:class_name], :class=>opts[:class], :block=>block) 520: opts[:orig_opts] = orig_opts 521: # don't add to association_reflections until we are sure there are no errors 522: association_reflections[name] = opts 523: end
The association reflection hash for the association of the given name.
# File lib/sequel/model/associations.rb, line 526 526: def association_reflection(name) 527: association_reflections[name] 528: end
# File lib/sequel/model/deprecated.rb, line 187 187: def belongs_to(*args, &block) 188: Deprecation.deprecate('Sequel::Model.belongs_to', 'Use Sequel::Model.many_to_one') 189: many_to_one(*args, &block) 190: end
Modify and return eager loading dataset based on association options
# File lib/sequel/model/associations.rb, line 536 536: def eager_loading_dataset(opts, ds, select, associations) 537: ds = ds.select(*select) if select 538: ds = ds.filter(opts[:conditions]) if opts[:conditions] 539: ds = ds.order(*opts[:order]) if opts[:order] 540: ds = ds.eager(opts[:eager]) if opts[:eager] 541: ds = ds.eager_graph(opts[:eager_graph]) if opts[:eager_graph] 542: ds = ds.eager(associations) unless Array(associations).empty? 543: ds = opts[:eager_block].call(ds) if opts[:eager_block] 544: ds 545: end
# File lib/sequel/model/deprecated.rb, line 197 197: def has_and_belongs_to_many(*args, &block) 198: Deprecation.deprecate('Sequel::Model.has_and_belongs_to_many', 'Use Sequel::Model.many_to_many') 199: many_to_many(*args, &block) 200: end
# File lib/sequel/model/deprecated.rb, line 192 192: def has_many(*args, &block) 193: Deprecation.deprecate('Sequel::Model.has_many', 'Use Sequel::Model.one_to_many') 194: one_to_many(*args, &block) 195: end
Copy the association reflections to the subclass
# File lib/sequel/model/associations.rb, line 548 548: def inherited(subclass) 549: super 550: subclass.instance_variable_set(:@association_reflections, @association_reflections.dup) 551: end
Shortcut for adding a many_to_many association, see associate
# File lib/sequel/model/associations.rb, line 554 554: def many_to_many(*args, &block) 555: associate(:many_to_many, *args, &block) 556: end
Shortcut for adding a many_to_one association, see associate
# File lib/sequel/model/associations.rb, line 559 559: def many_to_one(*args, &block) 560: associate(:many_to_one, *args, &block) 561: end
Shortcut for adding a one_to_many association, see associate
# File lib/sequel/model/associations.rb, line 564 564: def one_to_many(*args, &block) 565: associate(:one_to_many, *args, &block) 566: end