Daniel Hoelzgen · @dhoelzgen
31 followers · 9 posts · Server ruhr.social

To allow eager loading scoped associations that require a parameter in my on models, I use a global store to provide the info I need. It's a bit clunky, but Preloaders would not allow chaining.

#ruby #rails #activerecord

Last updated 1 year ago

Daniel Hoelzgen · @dhoelzgen
31 followers · 9 posts · Server ruhr.social

To allow eager loading scoped associations that require a parameter in my on 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?

#ruby #rails #activerecord

Last updated 1 year ago

Stephann V. · @ste
19 followers · 152 posts · Server ruby.social

If you don't know `scope` accepts classes in place of blocks. so you can organize complex queries logic in their own class files.

#rails #activerecord

Last updated 1 year ago

ConstantOrbit · @constantorbit
971 followers · 2424 posts · Server hachyderm.io

okay, it's not just me... the is just really poorly designed. Really frustrating for those of us coming from an 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.

futurefoundry.co/blog/mongoose

#mongoose #middleware #activerecord #nodejs #mongodb

Last updated 1 year ago

postmodern · @postmodern
1329 followers · 1992 posts · Server ruby.social

Do people prefer sub-classing with default scopes and a type enum column to STI sub-classes with a type/class column?

#activerecord

Last updated 1 year ago

jakswa · @jakswa
15 followers · 77 posts · Server fosstodon.org

I'm always curating my harsh feelings on "after commit, queue background job" patterns. Directed at myself the most!

Is your method buried in a transaction? Is there a sneaky `after_X` callback doing gnarly things? Is there a state machine 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 😹

#rails #activerecord #ruby

Last updated 1 year ago

FastRuby.io · @FastRuby
47 followers · 158 posts · Server ruby.social

🚨 One line of poorly written can expose sensitive data.

Ensure your Rails application's security with FastRuby's comprehensive audit. Dive in and safeguard your app: fastruby.io/security-audit?utm

#activerecord #datasecurity #SecurityAudit #rubyonrails #railsapplication

Last updated 1 year ago

postmodern · @postmodern
1320 followers · 1935 posts · Server ruby.social

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

Last updated 1 year ago

FastRuby.io · @FastRuby
41 followers · 135 posts · Server ruby.social
postmodern · @postmodern
1315 followers · 1874 posts · Server ruby.social

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

Last updated 1 year ago

James Jefferies · @jamesjefferies
248 followers · 965 posts · Server mastodon.me.uk

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

Last updated 1 year ago

postmodern · @postmodern
1307 followers · 1856 posts · Server ruby.social

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

Last updated 1 year ago

postmodern · @postmodern
1303 followers · 1833 posts · Server ruby.social

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

Last updated 1 year ago

postmodern · @postmodern
1294 followers · 1819 posts · Server ruby.social

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.
github.com/rails/rails/blob/v7

#activerecord

Last updated 1 year ago

christine · @christine
280 followers · 277 posts · Server ruby.social

If you are doing a bulk load of Postgres data through , 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.

#activerecord

Last updated 1 year ago

Daniel 🐙 Deboer · @dandb
133 followers · 2464 posts · Server mas.to

I go either way on stuff like vs or whatever, but this is the first framework design I've seen that's just unqualified Bad Design.

#datamapper #activerecord

Last updated 1 year ago

junkman · @junkman
24 followers · 306 posts · Server mstdn.mx

Is there a way to call a DB function via ? Maybe ? E.g.:

`my_boolean = ActiveRecord::Base.call("login_ok(?, ?)", username, password)`

I'm trying to use on an existing database

#activerecord #sequel #rubyonrails

Last updated 2 years ago

postmodern · @postmodern
1222 followers · 1487 posts · Server ruby.social

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?
apidock.com/rails/v5.2.3/Activ

#activerecord

Last updated 2 years ago

postmodern · @postmodern
1215 followers · 1456 posts · Server ruby.social

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

Last updated 2 years ago

postmodern · @postmodern
1212 followers · 1427 posts · Server ruby.social

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)
)
```

#activerecord

Last updated 2 years ago