1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

hm:t join tables may not have a primary key

This commit is contained in:
Aaron Patterson 2013-09-06 17:14:07 -07:00
parent a9da99ee5f
commit 6a91a3307f
2 changed files with 23 additions and 1 deletions

View file

@ -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

View file

@ -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