added :piggyback option to has_many :through relationships to pick up values from the join table as needed
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3323 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
1235d26bfb
commit
1d2c6ee8f3
@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* added :piggyback option to has_many :through relationships to pick up values from the join table as needed [Tobias Luetke]
|
||||
|
||||
* Fix typo in association docs. #3296. [Blair Zajac]
|
||||
|
||||
* Fixed :through relations when using STI inherited classes would use the inherited class's name as foreign key on the join model [Tobias Luetke]
|
||||
|
@ -881,7 +881,7 @@ def create_has_many_reflection(association_id, options, &extension)
|
||||
:class_name, :table_name, :foreign_key,
|
||||
:exclusively_dependent, :dependent,
|
||||
:select, :conditions, :include, :order, :group, :limit, :offset,
|
||||
:as, :through,
|
||||
:as, :through, :piggyback,
|
||||
:finder_sql, :counter_sql,
|
||||
:before_add, :after_add, :before_remove, :after_remove,
|
||||
:extend
|
||||
|
@ -39,7 +39,7 @@ def method_missing(method, *args, &block)
|
||||
|
||||
def find_target
|
||||
@reflection.klass.find(:all,
|
||||
:select => "#{@reflection.table_name}.*",
|
||||
:select => construct_select,
|
||||
:conditions => construct_conditions,
|
||||
:from => construct_from,
|
||||
:order => @reflection.options[:order],
|
||||
@ -73,6 +73,14 @@ def construct_from
|
||||
"#{@reflection.table_name}, #{@owner.class.reflections[@reflection.options[:through]].table_name}"
|
||||
end
|
||||
|
||||
def construct_select
|
||||
selected = ["#{@reflection.table_name}.*"]
|
||||
if @reflection.options[:piggyback]
|
||||
selected += [@reflection.options[:piggyback]].flatten.collect { |field| "#{@owner.class.reflections[@reflection.options[:through]].table_name}.#{field}" }
|
||||
end
|
||||
selected.join(', ')
|
||||
end
|
||||
|
||||
def construct_scope
|
||||
{
|
||||
:find => { :conditions => construct_conditions },
|
||||
|
@ -38,5 +38,9 @@ def test_polymorphic_has_many_going_through_join_model
|
||||
def test_polymorphic_has_many_going_through_join_model_with_inheritance
|
||||
assert_equal tags(:general), posts(:thinking).tags.first
|
||||
end
|
||||
|
||||
def test_has_many_with_piggyback
|
||||
assert_equal "2", categories(:sti_test).authors.first.post_id
|
||||
end
|
||||
|
||||
end
|
||||
|
2
activerecord/test/fixtures/category.rb
vendored
2
activerecord/test/fixtures/category.rb
vendored
@ -6,7 +6,7 @@ def self.what_are_you
|
||||
end
|
||||
|
||||
has_many :categorizations
|
||||
has_many :authors, :through => :categorizations
|
||||
has_many :authors, :through => :categorizations, :piggyback => [:post_id]
|
||||
end
|
||||
|
||||
class SpecialCategory < Category
|
||||
|
Loading…
Reference in New Issue
Block a user