mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Autosave False On ActiveRecord Associations as a rails til.
This commit is contained in:
@@ -102,6 +102,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [All or Nothing Database Transactions](rails/all-or-nothing-database-transactions.md)
|
- [All or Nothing Database Transactions](rails/all-or-nothing-database-transactions.md)
|
||||||
- [Attribute Getter without the Recursion](rails/attribute-getter-without-the-recursion.md)
|
- [Attribute Getter without the Recursion](rails/attribute-getter-without-the-recursion.md)
|
||||||
- [Attribute Was](rails/attribute-was.md)
|
- [Attribute Was](rails/attribute-was.md)
|
||||||
|
- [Autosave False On ActiveRecord Associations](rails/autosave-false-on-activerecord-associations.md)
|
||||||
- [Capybara Page Status Code](rails/capybara-page-status-code.md)
|
- [Capybara Page Status Code](rails/capybara-page-status-code.md)
|
||||||
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
||||||
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
||||||
|
|||||||
44
rails/autosave-false-on-activerecord-associations.md
Normal file
44
rails/autosave-false-on-activerecord-associations.md
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# Autosave False On ActiveRecord Associations
|
||||||
|
|
||||||
|
A relationship between two ActiveRecord models can be established with a
|
||||||
|
`has_one` or `has_many` association. This relationship has some
|
||||||
|
implications. By default, saving a record will also save the associated
|
||||||
|
records that have since been built. Consider this example of users that have
|
||||||
|
many posts (`has_many posts`).
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> u = User.first
|
||||||
|
#=> #<User ...>
|
||||||
|
> u.posts
|
||||||
|
#=> []
|
||||||
|
> u.posts.build(title: "Some Title", content: "This is a post")
|
||||||
|
#=> #<Post ...>
|
||||||
|
> u.save
|
||||||
|
#=> true
|
||||||
|
> u.posts(reload: true)
|
||||||
|
#=> [#<Post ...>]
|
||||||
|
```
|
||||||
|
|
||||||
|
When the user is saved, the associated post that was built for that user
|
||||||
|
also gets saved to the database.
|
||||||
|
|
||||||
|
If the association is instead defined with the `autosave` option set to
|
||||||
|
false, then saving a record will not cause associated records to also be
|
||||||
|
saved. The associated records will need to be saved explicitly. Consider the
|
||||||
|
same example from above, but with `has_many posts, autosave: false`.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> u = User.first
|
||||||
|
#=> #<User ...>
|
||||||
|
> u.posts
|
||||||
|
#=> []
|
||||||
|
> u.posts.build(title: "Some Title", content: "This is a post")
|
||||||
|
#=> #<Post ...>
|
||||||
|
> u.save
|
||||||
|
#=> true
|
||||||
|
> u.posts(reload: true)
|
||||||
|
#=> []
|
||||||
|
```
|
||||||
|
|
||||||
|
The post wasn't saved with the user and it wasn't saved explicitly, so it
|
||||||
|
isn't persisted to the database.
|
||||||
Reference in New Issue
Block a user