diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-06 17:14:07 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-06 17:14:07 -0700 |
commit | 6a91a3307ff6556225ab8717617074cea20222e0 (patch) | |
tree | 43598080eea7d8ebae994cba7ab0cd1b3e17f4e3 /activerecord | |
parent | a9da99ee5fd73b6d52c6fb443b2443cdfb262d5f (diff) | |
download | rails-6a91a3307ff6556225ab8717617074cea20222e0.tar.gz rails-6a91a3307ff6556225ab8717617074cea20222e0.tar.bz2 rails-6a91a3307ff6556225ab8717617074cea20222e0.zip |
hm:t join tables may not have a primary key
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_through_associations_test.rb | 18 |
2 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 607ed0da46..a3fcca8a27 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -125,7 +125,11 @@ module ActiveRecord end def foreign_key_present? - owner.attribute_present?(reflection.association_primary_key) + if reflection.klass.primary_key + owner.attribute_present?(reflection.association_primary_key) + else + false + end end end end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 508faa9437..541f649ae9 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -64,6 +64,24 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase anonbook.subscribers.map(&:id).sort end + def test_no_pk_join_table_append + lesson = make_model 'Lesson' + student = make_model 'Student' + + lesson_student = make_model 'LessonStudent' + lesson_student.table_name = 'lessons_students' + + lesson_student.belongs_to :lesson, :class => lesson + lesson_student.belongs_to :student, :class => student + lesson.has_many :lesson_students, :class => lesson_student + lesson.has_many :students, :through => :lesson_students, :class => student + + sicp = lesson.new(:name => "SICP") + ben = student.new(:name => "Ben Bitdiddle") + sicp.students << ben + assert sicp.save! + end + def test_pk_is_not_required_for_join post = Post.includes(:scategories).first post2 = Post.includes(:categories).first |