Permalink
Browse files

hm:t join tables may not have a primary key

  • Loading branch information...
1 parent a9da99e commit 6a91a3307ff6556225ab8717617074cea20222e0 @tenderlove tenderlove committed Sep 7, 2013
@@ -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
@@ -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

0 comments on commit 6a91a33

Please sign in to comment.