Errors can be indexed with nested attributes#19686
Conversation
There was a problem hiding this comment.
Will change, but isn't it indices?
There was a problem hiding this comment.
Let's use || and ! here to be in line with our style guide 😄
There was a problem hiding this comment.
Done. We'll update once we figured out how we should break the conditional up (see above).
695b20b to
b8cbac1
Compare
`has_many` can now take `index_errors: true` as an option. When this is enabled, errors for nested models will be returned alongside an index, as opposed to just the nested model name. This option can also be enabled (or disabled) globally through `ActiveRecord::Base.index_nested_attribute_errors` E.X. ```ruby class Guitar < ActiveRecord::Base has_many :tuning_pegs accepts_nested_attributes_for :tuning_pegs end class TuningPeg < ActiveRecord::Base belongs_to :guitar validates_numericality_of :pitch end ``` - Old style - `guitar.errors["tuning_pegs.pitch"] = ["is not a number"]` - New style (if defined globally, or set in has_many_relationship) - `guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]` [Michael Probber, Terence Sun]
Errors can be indexed with nested attributes Close #8638
There was a problem hiding this comment.
No, that is the correct way to spell its here
On Mon, Oct 26, 2015, 5:20 PM Shunsuke Aida notifications@github.com
wrote:
In activerecord/CHANGELOG.md
#19686 (comment):@@ -1,3 +1,31 @@
+* Add option to index errors in nested attributes
+
- For models which have nested attributes, errors within those models will
- now be indexed if :index_errors is specified when defining a
- has_many relationship, or if its set in the global config.
typo of it's?
—
Reply to this email directly or view it on GitHub
https://github.com/rails/rails/pull/19686/files#r43067124.
There was a problem hiding this comment.
It's best you check its meaning 🤓
There was a problem hiding this comment.
Consider a user reading this. Where would you find "global config?"
There was a problem hiding this comment.
its indicates possession.
it's is a contraction for it is.
The latter seems to be the case here. If so, it is would be even better.
|
Excellent stuff guys! |
|
pulled it in on top of 4.2.4 and it works like a charm. Would love to see this change being in 4.2.5 so I can discard my fork :) |
|
Sorry, we don't backport features to patch releases. This'll be in Rails 5. |
|
Long overdue. Thanks for your effort. 🏁 |
* Using an instance variable allows JS populate operations to have
access to the @order from within the rendered .js.erb files
* Assign all line_item errors to the order's base
* Allow to respond to JS requests for `#populate` and `#update` actions.
It is left for the developer to actually implement the `js.erb` views
To consider / TODO:
- to avoid the whole rescue operation, and manually assigning line item
errors to the order, we'd need to do to be doing `order.save`
instead of `line_item.save!` in [`OrderContents`](https://github.com/solidusio/solidus/blob/master/core/app/models/spree/order_contents.rb#L151)
-> That would have the effect to "automatically" assign the line item
errors on the order in the form: `line_items.quantity` in Rails 4.x
and "line_items[0].quantity" in Rails 5
More info:
- rails/rails#8638
- rails/rails#19686
|
I can't find a way to parse my errors messages: errors = {
"categories[0].listener_deadline_values[0].value": ["can't be blank"],
"categories[0].listener_deadline_values[0].deadline": ["can't be blank"],
"categories[0].name": ["can't be blank"],
"categories[1].listener_deadline_values[0].value": ["can't be blank"],
"categories[1].listener_deadline_values[0].deadline": ["can't be blank"],
"categories[1].name": ["can't be blank"],
"categories[2].listener_deadline_values[0].value": ["can't be blank"],
"categories[2].listener_deadline_values[0].deadline": ["can't be blank"],
"categories[2].name": ["can't be blank"]
}I would expect to be able to access these errors like How do you parse it? I discovered how: https://medium.com/@kdiogenes/giving-super-powers-to-rails-nested-forms-with-vue-js-part-2-acee4a3ee43d |
|
Is there a way to index by another criteria @mprobber ? Like the ID of the nested model if it's not a new record? This is because i can't figure a way when using rails 5 api to map errors into several nested children which some of them can have errors while others not, therefore indexes won't match one on one. Or is there another clever solution to handle this situation? |
|
@tommyalvarez |
|
@vidurangaw i ended up writing my own custom NestedErrorSerializer (i was using active model serializer with json-api adapter). This way i was able to return the errors the way i wanted, respeting json-api format. Thought it would take me a lot of time but it wasn't that hard at all. Here is the implementation i made: Then in the controller i used them like:
|
|
@tommyalvarez |
|
Why this option is not mentioned in the official doc ? |
|
I've created a gem (active_record-nested_error_indexer) to monkey patch these changes to Rails 4 😄 |
accepts_nested_attributes_for can now take index_errors: true as an
option. When this is enabled, errors for nested models will be
returned alongside an index, as opposed to just the nested model name.
This option can also be enabled globally in a configuration file.
@tsun1215
#8638