Ruby on Rails: question about validates_presence_of

后端 未结 3 2217
离开以前
离开以前 2021-02-20 04:28

I have a relationship in my ActiveRecord based model that looks like:

belongs_to :foo

My model should always have foo defined in it for it to b

相关标签:
3条回答
  • 2021-02-20 05:12

    You may want to consider validates_associated

    This will make sure that the association is valid.

    0 讨论(0)
  • 2021-02-20 05:21

    The first one: validates_presence_of :foo (though I think the second one will work too). In general the Ruby validation helpers work on model names as opposed to explicit column names. You can use this in addition to validates_associated to both validate the state of the associated entity in addition to its presence.

    0 讨论(0)
  • 2021-02-20 05:27

    I use validates_presence_of :foo_id. It works.

    0 讨论(0)
提交回复
热议问题