Class: BigRecord::ArAssociations::HasManyAssociation
- Inherits:
-
AssociationCollection
- Object
- AssociationProxy
- AssociationCollection
- BigRecord::ArAssociations::HasManyAssociation
- Defined in:
- lib/big_record/ar_associations/has_many_association.rb
Overview
:nodoc:
Instance Method Summary
- - (Object) build(attributes = {})
-
- (Object) count(*args)
Count the number of associated records.
- - (Object) find(*args)
-
- (HasManyAssociation) initialize(owner, reflection)
constructor
A new instance of HasManyAssociation.
Methods inherited from AssociationCollection
#<<, #clear, #create, #delete, #destroy_all, #empty?, #length, #replace, #reset, #size, #sum, #to_ary, #uniq
Methods inherited from AssociationProxy
#===, #aliased_table_name, #conditions, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_target, #reload, #reset, #respond_to?, #target, #target=
Constructor Details
- (HasManyAssociation) initialize(owner, reflection)
A new instance of HasManyAssociation
4 5 6 7 |
# File 'lib/big_record/ar_associations/has_many_association.rb', line 4 def initialize(owner, reflection) super construct_sql end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method, *args, &block) (protected)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/big_record/ar_associations/has_many_association.rb', line 77 def method_missing(method, *args, &block) if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) super else create_scoping = {} set_belongs_to_association_for(create_scoping) @reflection.klass.with_scope( :create => create_scoping, :find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false } ) do @reflection.klass.send(method, *args, &block) end end end |
Instance Method Details
- (Object) build(attributes = {})
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/big_record/ar_associations/has_many_association.rb', line 9 def build(attributes = {}) if attributes.is_a?(Array) attributes.collect { |attr| build(attr) } else record = @reflection.klass.new(attributes) set_belongs_to_association_for(record) @target ||= [] unless loaded? @target << record record end end |
- (Object) count(*args)
Count the number of associated records. All arguments are optional.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/big_record/ar_associations/has_many_association.rb', line 24 def count(*args) if @reflection.[:counter_sql] @reflection.klass.count_by_sql(@counter_sql) elsif @reflection.[:finder_sql] @reflection.klass.count_by_sql(@finder_sql) else column_name, = @reflection.klass.send(:construct_count_options_from_legacy_args, *args) [:conditions] = [:conditions].nil? ? @finder_sql : @finder_sql + " AND (#{sanitize_sql(options[:conditions])})" [:include] = @reflection.[:include] @reflection.klass.count(column_name, ) end end |
- (Object) find(*args)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/big_record/ar_associations/has_many_association.rb', line 40 def find(*args) = Base.send(:extract_options_from_args!, args) # If using a custom finder_sql, scan the entire collection. if @reflection.[:finder_sql] expects_array = args.first.kind_of?(Array) ids = args.flatten.compact.uniq if ids.size == 1 id = ids.first record = load_target.detect { |record| id == record.id } expects_array ? [ record ] : record else load_target.select { |record| ids.include?(record.id) } end else conditions = "#{@finder_sql}" if sanitized_conditions = sanitize_sql([:conditions]) conditions << " AND (#{sanitized_conditions})" end [:conditions] = conditions if [:order] && @reflection.[:order] [:order] = "#{options[:order]}, #{@reflection.options[:order]}" elsif @reflection.[:order] [:order] = @reflection.[:order] end () # Pass through args exactly as we received them. args << @reflection.klass.find(*args) end end |