Rails: How to limit number of items in has_many association (from Parent)

后端 未结 8 1185
情深已故
情深已故 2021-01-30 11:45

I would like to limit the number of items in an association. I want to ensure the User doesn\'t have more than X Things. This question was asked before and the solution had th

8条回答
  •  误落风尘
    2021-01-30 12:28

    How you investigated using accepts_nested_attributes_for?

    accepts_nested_attributes_for :things, :limit => 5

    http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    That said, I think accepts_nested_attributes_for seems to only be appropriate for certain types of situations. For example, if you were creating a command line API, I think it's a pretty awful solution. However, if you have a nested form it works well enough (most of the time).

提交回复
热议问题