Skip to content

Commit e1344bf

Browse files
committed
Tidy up previous commit.
1 parent f8b53f3 commit e1344bf

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,11 +1437,11 @@ def collection_accessor_methods(reflection, association_proxy_class, writer = tr
14371437
association.replace(new_value)
14381438
association
14391439
end
1440-
1440+
14411441
redefine_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
1442-
pk_column = reflection.klass.columns.find{|c| c.name == reflection.klass.primary_key }
1442+
pk_column = reflection.primary_key_column
14431443
ids = (new_value || []).reject { |nid| nid.blank? }
1444-
ids.map!{|i| pk_column.type_cast(i)}
1444+
ids.map!{ |i| pk_column.type_cast(i) }
14451445
send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
14461446
end
14471447
end

activerecord/lib/active_record/reflection.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,19 @@ def initialize(macro, name, options, active_record)
9191
#
9292
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>:balance</tt>
9393
# <tt>has_many :clients</tt> returns <tt>:clients</tt>
94-
def name
95-
@name
96-
end
94+
attr_reader :name
9795

9896
# Returns the macro type.
9997
#
10098
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>:composed_of</tt>
10199
# <tt>has_many :clients</tt> returns <tt>:has_many</tt>
102-
def macro
103-
@macro
104-
end
100+
attr_reader :macro
105101

106102
# Returns the hash of options used for the macro.
107103
#
108104
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>{ :class_name => "Money" }</tt>
109105
# <tt>has_many :clients</tt> returns +{}+
110-
def options
111-
@options
112-
end
106+
attr_reader :options
113107

114108
# Returns the class for the macro.
115109
#
@@ -137,11 +131,6 @@ def sanitized_conditions #:nodoc:
137131
@sanitized_conditions ||= klass.send(:sanitize_sql, options[:conditions]) if options[:conditions]
138132
end
139133

140-
# Returns +true+ if +self+ is a +belongs_to+ reflection.
141-
def belongs_to?
142-
macro == :belongs_to
143-
end
144-
145134
private
146135
def derive_class_name
147136
name.to_s.camelize
@@ -213,6 +202,10 @@ def primary_key_name
213202
@primary_key_name ||= options[:foreign_key] || derive_primary_key_name
214203
end
215204

205+
def primary_key_column
206+
@primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
207+
end
208+
216209
def association_foreign_key
217210
@association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
218211
end
@@ -307,6 +300,11 @@ def dependent_conditions(record, base_class, extra_conditions)
307300
dependent_conditions
308301
end
309302

303+
# Returns +true+ if +self+ is a +belongs_to+ reflection.
304+
def belongs_to?
305+
macro == :belongs_to
306+
end
307+
310308
private
311309
def derive_class_name
312310
class_name = name.to_s.camelize

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
2222
fixtures :posts, :readers, :people, :comments, :authors,
2323
:owners, :pets, :toys, :jobs, :references, :companies,
24-
:subscribers, :books, :subscriptions
24+
:subscribers, :books, :subscriptions, :developers
2525

2626
# Dummies to force column loads so query counts are clean.
2727
def setup

0 commit comments

Comments
 (0)