Hi!
I wanted to get some feedback on the idea of enhancing the ActiveRecord `
attributes` method to also (optionally) return nested attributes
Example
Let's say I have a `Car` model, which `has_one` `Driver` and `has_many` `
Passenger`(s).
Also let's say the `Car` model `accepts_nested_attributes` for both the `
Driver` and `Passenger` models.
A normal call to `@car.attributes` might result in
{
id: "5",
make: "Saab",
model: "9-3",
year: "2008",
automatic_transmission: true
}
The change would be to allow `attributes` to also return the nested
sub-hashes. So `@car.attributes(include_nested: true)` would result in
something like
{
id: "5",
make: "Saab",
model: "9-3",
year: "2008",
automatic_transmission: true
driver_attributes: {
name: "Foo Bar",
age: 27
},
passengers_attributes: {
"0" => {
name: "Foo Bar1",
height_cm: 170
},
"1" => {
name: "Foo Bar2",
height_cm: 195
}
}
}
When working with Rails forms or form tests it seems handy to have the
ability to generate all the attributes. This mirrors the behavior of
`attributes=` which can read a nested attributes hash as well.
This change would also follow the action* gem standard of whitelisting and
blacklisting certain associations
@car.attributes(include_nested: { only: [:passengers] })
@car.attributes(include_nested: { except: [:passengers] })
This is my first time contributing to the rails core, so would love to know
what everyone thinks.
Thanks!
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.