- Jul 2020
-
github.com github.com
-
[:returning] (Postgres-only) An array of attributes that should be returned for all successfully inserted records. For databases that support INSERT ... RETURNING, this will default to returning the primary keys of the successfully inserted records. Pass returning: %w[ id name ] to return the id and name of every successfully inserted record or pass returning: false to omit the clause.
-
- Jun 2020
-
medium.com medium.com
-
If those comments are loaded outside of the blog_post association, then attempting to reference the blog_post association from within each comment will result in N blog_posts table queries even if they all belong to the same BlogPost!
-
-
wangjohn.github.io wangjohn.github.io
-
In this case, we notice that comment.post and post should belong to the same database object. But, is Rails smart enough to know that the comment should be removed from both of the associations? Or are comment.post and post different representations of the same database row?
-
-
api.rubyonrails.org api.rubyonrails.org
-
transaction calls can be nested. By default, this makes all database statements in the nested transaction block become part of the parent transaction. For example, the following behavior may be surprising: User.transaction do User.create(username: 'Kotori') User.transaction do User.create(username: 'Nemu') raise ActiveRecord::Rollback end end creates both “Kotori” and “Nemu”. Reason is the ActiveRecord::Rollback exception in the nested block does not issue a ROLLBACK. Since these exceptions are captured in transaction blocks, the parent block does not see it and the real transaction is committed.
How is this okay??
When would it ever be the desired/intended behavior for a
raise ActiveRecord::Rollback
to have absolutely no effect? What good is the transaction then??What happened to the principle of least surprise?
Is there any reason we shouldn't just always use
requires_new: true
?If, like they say, the inner transaction "become[s] part of the parent transaction", then if anything, it should roll back the parent transaction too — not roll back nothing.
-
One workaround is to begin a transaction on each class whose models you alter:
-
- Apr 2020
-
stackoverflow.com stackoverflow.com
-
gist.github.com gist.github.com
- Jan 2020
-
web.archive.org web.archive.org
-
I love merge too. Wow, it's been around a while.
> ? merge From: ~/.gem/ruby/2.4.5/gems/activerecord-5.2.2/lib/active_record/relation/spawn_methods.rb @ line 14: Owner: ActiveRecord::SpawnMethods Visibility: public Signature: merge(other) Number of lines: 17 Merges in the conditions from other, if other is an ActiveRecord::Relation. Returns an array representing the intersection of the resulting records with other, if other is an array.
-
-
buildingvts.com buildingvts.com
-
table name was not specified in the default scope definition. Since ActiveRecord does not canonically reference fields, after inner joining with another table another_table.term_id column can’t be found.
-
-
-
You might be thinking––"a tool that allows me to write semantic and reusable queries? Sounds like Active Record". It's absolutley true that Active Record already provides a powerful query tool kit. But what happens when even simple queries stretch the bounds of Active Record's capabilities?
-
-
github.com github.com
-
github.com github.com
-
guides.rubyonrails.org guides.rubyonrails.org
-
before_destroy callbacks should be placed before dependent: :destroy associations (or use the prepend: true option), to ensure they execute before the records are deleted by dependent: :destroy.
-
-
github.com github.com
-
github.com github.com
-
for pointing me to the spiritual successor to this project at: https://github.com/GeorgeKaraszi/ActiveRecordExtended
-
-
stackoverflow.com stackoverflow.com
-
Event.joins(:packages).having('array_agg(packages.type) @> array[?]', packages).group(:id)
-
-
stackoverflow.com stackoverflow.com
-
This illustrates a pretty common challenge with joins: you want them to be used for some of the query but not for other of it.
-
-
gist.github.com gist.github.com
Tags
Annotators
URL
-
- Dec 2019
-
github.com github.com
- Aug 2019
-
github.com github.com
-
pluck_to_hash
-