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

前端 未结 6 1225
夕颜
夕颜 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 18:09

    Instead of validating the presence of the article's id you could validate the presence of the article.

    validates_presence_of :article
    

    Then when you are creating your comment:

    article.comments.build :article => article
    

提交回复
热议问题