diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-04-06 02:18:42 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-04-06 02:18:42 +0000 |
commit | 9bc75fd007d56d818b8620569410a20aa92c9fc5 (patch) | |
tree | 750bf397569d4fdf20eb6285d68330b9efe91a56 /activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | |
parent | f6b12c11cd3a6df8525dd16ec093ec473813489e (diff) | |
download | rails-9bc75fd007d56d818b8620569410a20aa92c9fc5.tar.gz rails-9bc75fd007d56d818b8620569410a20aa92c9fc5.tar.bz2 rails-9bc75fd007d56d818b8620569410a20aa92c9fc5.zip |
Remove duplicate code from associations. [Pratik]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9231 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 53 |
1 files changed, 4 insertions, 49 deletions
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 8ce5b83831..d4143e0645 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -1,11 +1,6 @@ module ActiveRecord module Associations class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc: - def initialize(owner, reflection) - super - construct_sql - end - def create(attributes = {}) create_record(attributes) { |record| insert_record(record) } end @@ -14,53 +9,13 @@ module ActiveRecord create_record(attributes) { |record| insert_record(record, true) } end - def find_first - load_target.first - end - - def find(*args) - options = args.extract_options! - - # If using a custom finder_sql, scan the entire collection. - if @reflection.options[: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 { |r| id == r.id } - expects_array ? [record] : record - else - load_target.select { |r| ids.include?(r.id) } - end - else - conditions = "#{@finder_sql}" - - if sanitized_conditions = sanitize_sql(options[:conditions]) - conditions << " AND (#{sanitized_conditions})" - end - - options[:conditions] = conditions + protected + def construct_find_options!(options) options[:joins] = @join_sql options[:readonly] = finding_with_ambiguous_select?(options[:select] || @reflection.options[:select]) - - if options[:order] && @reflection.options[:order] - options[:order] = "#{options[:order]}, #{@reflection.options[:order]}" - elsif @reflection.options[:order] - options[:order] = @reflection.options[:order] - end - - merge_options_from_reflection!(options) - - options[:select] ||= (@reflection.options[:select] || '*') - - # Pass through args exactly as we received them. - args << options - @reflection.klass.find(*args) + options[:select] ||= (@reflection.options[:select] || '*') end - end - - protected + def count_records load_target.size end |