Class: BigRecord::BrAssociations::HasAndBelongsToManyAssociation
- Inherits:
-
AssociationCollection
- Object
- AssociationProxy
- AssociationCollection
- BigRecord::BrAssociations::HasAndBelongsToManyAssociation
- Defined in:
- lib/big_record/br_associations/has_and_belongs_to_many_association.rb
Overview
:nodoc:
Instance Method Summary
- - (Object) build(attributes = {})
- - (Object) create(attributes = {})
- - (Object) create!(attributes = {})
- - (Object) find(*args)
- - (Object) find_first
-
- (HasAndBelongsToManyAssociation) initialize(owner, reflection)
constructor
A new instance of HasAndBelongsToManyAssociation.
Methods inherited from AssociationCollection
#<<, #clear, #delete, #destroy_all, #empty?, #length, #replace, #reset, #size, #sum, #to_ary, #uniq
Methods inherited from AssociationProxy
#===, #aliased_table_name, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_target, #reload, #reset, #respond_to?, #target, #target=
Constructor Details
- (HasAndBelongsToManyAssociation) initialize(owner, reflection)
A new instance of HasAndBelongsToManyAssociation
4 5 6 7 |
# File 'lib/big_record/br_associations/has_and_belongs_to_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 in the class BigRecord::BrAssociations::AssociationProxy
Instance Method Details
- (Object) build(attributes = {})
9 10 11 12 |
# File 'lib/big_record/br_associations/has_and_belongs_to_many_association.rb', line 9 def build(attributes = {}) load_target build_record(attributes) end |
- (Object) create(attributes = {})
14 15 16 |
# File 'lib/big_record/br_associations/has_and_belongs_to_many_association.rb', line 14 def create(attributes = {}) create_record(attributes) { |record| insert_record(record) } end |
- (Object) create!(attributes = {})
18 19 20 |
# File 'lib/big_record/br_associations/has_and_belongs_to_many_association.rb', line 18 def create!(attributes = {}) create_record(attributes) { |record| insert_record(record, true) } end |
- (Object) find(*args)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 |
# File 'lib/big_record/br_associations/has_and_belongs_to_many_association.rb', line 26 def find(*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.to_i record = load_target.detect { |record| id == record.id } expects_array ? [record] : record else load_target.select { |record| ids.include?(record.id) } end else # Generate the sql query. The join table has to be in mysql. conditions = "#{@finder_sql}" # FIXME: throws "undefined method `abstract_class?' for Object:Class" # if sanitized_conditions = ActiveRecord::Base.send(:sanitize_sql, options[:conditions]) # conditions << " AND (#{sanitized_conditions})" # end if [:order] && @reflection.[:order] order = "#{options[:order]}, #{@reflection.options[:order]}" elsif @reflection.[:order] order = @reflection.[:order] end query = "SELECT #{@reflection.association_foreign_key} FROM #{@reflection.options[:join_table]} WHERE #{conditions} #{order};" # Execute the query ids = [] ActiveRecord::Base.connection.execute(query).each do |result| ids << result.first end # Find the big_record entries by id. Find them one at a time because duplicate entries must appear in # the result set if it's the case. The default find by ids in big_record does the same thing anyway... ids.collect{|id| @reflection.klass.find(id)} end end |
- (Object) find_first
22 23 24 |
# File 'lib/big_record/br_associations/has_and_belongs_to_many_association.rb', line 22 def find_first load_target.first end |