How can you validate the presence of a belongs to association with Rails?

前端 未结 6 1214
夕颜
夕颜 2020-12-28 17:05

Say I have a basic Rails app with a basic one-to-many relationship where each comment belongs to an article:

$ rails blog
$ cd blog
$ script/generate model a         


        
6条回答
  •  感情败类
    2020-12-28 17:46

    This fails because there is no identity map in Rails 2.3 or 3.0. You can manually fix it by stitching them together.

    a = Article.new
    c = a.comments.build
    c.article = a
    a.save!
    

    This is horrible, and what the identity map in 3.1 will help to fix (in addition to performance gains). c.article and a.comments.first.article are different objects without an identity map.

提交回复
热议问题