Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

hm:t join tables may not have a primary key

  • Loading branch information...
commit 6a91a3307ff6556225ab8717617074cea20222e0 1 parent a9da99e
@tenderlove tenderlove authored
View
6 activerecord/lib/active_record/associations/has_many_association.rb
@@ -125,7 +125,11 @@ def delete_records(records, method)
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
View
18 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -64,6 +64,24 @@ def test_singleton_has_many_through
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
Please sign in to comment.
Something went wrong with that request. Please try again.