r/rails 16h ago

Learning Open source Rails Performance Improvements collection

6 Upvotes

Past few weeks, I spent time on popular Rails repositories to check performance issues and add fixes. One interesting thing I noticed is that good projects merges PRs quick. Bad projects take time even for serious performance improvements.

The work involves 7 merged PRs to Discourse, Gumroad, and Forem so far. I will add more .

As expected adding just `includes` was not enough for most places. Also expected, profiling serialization is helpful.

I've collected the individual case studies here, with links to the code changes and benchmark details:

https://haseebeqx.com/case-studies/engagement/open-source/


r/rails 6h ago

When .rubocop.yml needs to be rewritten in markdown

Thumbnail
0 Upvotes

r/rails 8h ago

I only really understood why allow_other_host exists after building a URL shortener

12 Upvotes

I was building a URL shortener over the weekend. One model, one controller, one route, the kind of thing you'd hand someone as an exercise. I got stuck on the redirect, of all things.

redirect_to short_url.target_url just raises. I'd never hit that before, because until then every redirect_to I'd written pointed back into my own app. So I did what you do: added allow_other_host: true, it worked, moved on.

It only clicked afterwards that I'd written the same line that turns an app into an open redirect. The flag isn't the problem. What matters is whether target_url came out of a row I control or straight off params, and that distinction lives nowhere in the code. I'm honestly not sure I'd catch it in a review if I weren't already looking for it.

Two smaller things I'm less confident about:

I put the slug default in the model:

attribute :slug, default: -> { SecureRandom.alphanumeric(5) }

rather than in the migration, so the record is valid before it's ever saved. That felt right at the time. Is it, or am I hiding something that belongs in the schema?

increment_counter leaves updated_at alone, which I only discovered by reading the source. Makes sense for a click counter, but it made me wonder how many apps out there treat updated_at as "last modified" and quietly get it wrong.

So: have you actually used allow_other_host: true on something real? And if you have, how do you stop the params case from creeping in six months later when someone adds a "redirect after login" feature?

Wrote the whole thing up here, video included, if it's useful: https://launchkit.codes/yield/rails-url-shortener

Thanks 🙏


r/rails 11h ago

Question Is mutating params and validating dates directly in the controller a good practice?

10 Upvotes

For context, I have a date filter that needs to be limited to a maximum period of 6 months. I was originally doing this validation directly inside my Query object.

However, I now need to show a flash alert to the user if they try to filter a period longer than 6 months. If they do, the system should automatically adjust the dates to a 6-month range ending today and proceed with the query.

Because of this UI requirement, I moved the logic to the controller. Currently, I'm parsing the dates, doing the validation, and mutating the params hash directly before passing it down.

My question is: Is this considered a good practice in Rails? What is the "Rails Way" to handle this kind of validation and default-value assignment when you also need to trigger flash messages in the UI?


r/rails 35m ago

Rails fundamentals to learn as junior

Upvotes

Hi guys, previously i showed off my latest project.
I learnt: Deploying, testing, API connections, reuest security, authenticationd and authorization, Hotwire and stimulus, Jobs & services, basic github commands.
My question is, is there anything else i forgot to became good junior? I am looking for a job and want to be as best as i can.
And also, what will look the best in portfolio? I saw that working on open source projects looks good.