To allow eager loading scoped associations that require a parameter in my #Ruby on #Rails #ActiveRecord models, I use a global store to provide the info I need. It's a bit clunky, but Preloaders would not allow chaining.
To allow eager loading scoped associations that require a parameter in my #Ruby on #Rails #ActiveRecord models, I use a global store to provide the info I need. It's a bit clunky, but Preloaders would not allow chaining.
To avoid variable leak issues, I added an around action to the top-level controller, clearing the store after each request.
Is this the way to go, or does anyone know a better solution besides Preloaders?
If you don't know `scope` accepts classes in place of blocks. so you can organize complex queries logic in their own class files.
okay, it's not just me... the #mongoose #middleware is just really poorly designed. Really frustrating for those of us coming from an #activeRecord world where things are just a lot more mature.
Just wish I hadn't lost the hours I just spent banging my head against this. But at least I know now to avoid trying to do anything non-trivial in middleware.
#mongoose #middleware #activerecord #nodejs #mongodb
Do people prefer sub-classing with default scopes and a type enum column to STI sub-classes with a type/class column?
#activerecord
I'm always curating my harsh feelings on "after commit, queue background job" #rails patterns. Directed at myself the most!
Is your method buried in a transaction? Is there a sneaky `after_X` #activerecord callback doing gnarly things? Is there a state machine #ruby gem layering its own event callbacks on top?
...screw it I'll make this problem worse for people by copying over my random `model.post_commit(-> {})` helper that they'll need to learn 😹
🚨 One line of poorly written #ActiveRecord can expose sensitive data.
Ensure your Rails application's security with FastRuby's comprehensive audit. Dive in and safeguard your app: https://www.fastruby.io/security-audit?utm_source=mastodon&utm_medium=organic&utm_campaign=securityaudit&utm_term=&utm_content=textonly
#activerecord #datasecurity #SecurityAudit #rubyonrails #railsapplication
I'm considering adding models for storing street addresses, and I'm curious what the most efficient way to store them is? Should there be separate City, State, Country models, or just one StreetAddress model with city, state, country columns that repeat? I'd like to balance both query performance and storage space efficiency if possible.
#activerecord #normalization #schema #database
#activerecord #normalization #schema #database
You might have extended classes or instances in #Rails, but did you know you can also extend Rails associations? https://www.fastruby.io/blog/rails/how-to-extend-rails-associations.html?utm_source=Twitter&utm_medium=Tweet&utm_campaign=Rails&utm_term=extended-classes&utm_content=association&utm_id= #rubyonrails #activerecord
#rails #rubyonrails #activerecord
Is there some kind of new fancy ActiveRecord/Arel way to combine two `has_many` associations into a single `has_many :through` association or define a `has_many :through` association that goes through two separate `has_many` associations? I assume this could be done via SQL using multiple LEFT JOINs and another INNER JOIN?
#activerecord
I’ve a table with rows like:
Id:1, project_id: 12, dr: “01/01/2020”, q: 10, thing_id: 1
Id:2, project_id: 12, dr: “01/01/2021”, q: 12, thing_id: 1
id: 3, project_id: 7, dr: “01/01/2021”, q: 17, thing_id: 1
I want to sum the of thing_id: 1 quantities (q) for the *latest* record for a project, I thought this would work, but it includes all rows:
Klass.where(thing_id: thing_id)
.select('DISTINCT ON (project_id) *')
.order(:project_id, dr: :desc)
.sum(:quantity)
#Rails #ActiveRecord
How do you validate that at least one of three (or more) ActiveRecord belongs_to associations are set? Every example I can find mentions only two belongs_to associations.
#activerecord
Does ActiveRecord support the opposite of `dependent: :destroy`? What if I want to have Note records associated with data that is deleted and re-imported with the same primary keys.
#ruby #activerecord
ActiveRecord annoyance of the day: it uses `Dir.mkdir()` to create the sqlite3 file's parent directory instead of `FileUtils.mkdir_p()`. If you're attempting to create a new sqlite3 database in say ~/.cache/whatever/ and ~/.cache doesn't exist yet, it will fail.
https://github.com/rails/rails/blob/v7.0.6/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L32
#activerecord
If you are doing a bulk load of Postgres data through #activerecord , do not forget about those callbacks!! Loading 80k records with complex associations went from 638.29 seconds to 263.98 seconds. The only difference? Commenting out a very IO intensive 'after_save' callback.
I go either way on stuff like #ActiveRecord vs #DataMapper or whatever, but this is the first framework design I've seen that's just unqualified Bad Design.
Is there a way to call a DB function via #ActiveRecord ? Maybe #Sequel ? E.g.:
`my_boolean = ActiveRecord::Base.call("login_ok(?, ?)", username, password)`
I'm trying to use #RubyOnRails on an existing database
#activerecord #sequel #rubyonrails
I'm curious why ActiveRecord's model_name.human capitalizes the model's name to begin with? This seems awkward if you're embedding it within a sentence "Created 15 Foo bars" vs. "Created 15 foo bars". Is there a reason for capitalizing the human name of the model?
https://apidock.com/rails/v5.2.3/ActiveModel/Name/human
#activerecord
Noticing random ActiveRecord timeouts in my Sinatra+ActiveRecord app. Some Google/StackOverflow results say to increase the `pool:` number for the database configuration that's passed to `ActiveRecord::Base.establish_connection`. Others say to add `after { ActiveRecord::Base.clear_active_connections! }`. Which one is the correct/current way to solve this?
#activerecord
Wait, does `find_or_create_by` not work with other ActiveRecord::Base record objects? I'm trying to de-duplicate a new record which has many other belongs_to relationships, but it's not finding the existing record with the exact same foreign-key IDs.
```
MyModel.find_or_create_by(
foo: string_value,
bar: Bar.find_or_create_by(value: bar_value),
baz: Baz.find_or_create_by(value: baz_value)
)
```