Share a Rails Tip Weekly!

I’m trying to do a deeper dive into Rails internals and noticed the following.

Ok, this is kind of a funny one:

I was perusing the ActiveRecord::Querying module and noticed you can call methods like User.first (assuming you have a User model) all the way to User.fifth, and then I noticed you can’t do User.sixth but you can do User.forty_two

delegate :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, to: :all

Apparently, this was DHH’s cheeky response to redditors apparently

3 Likes

My Rails tip is remember that Rails is just Ruby, so you can create and use your own classes :slight_smile:

Just add to autoload in application.rb:

config.autoload_paths += %W(#{config.root}/app/classes)

6 Likes

I love that the :forty_two method is there, because it shows that Rails is a fun framework and community to work with.

I hate that it’s there, because it shows that they, the core Rails team, are not serious about getting performance optimisation for the entire suite.

Also, I agree that it should have been :forty_second.

2 Likes

If you’re talking numerical position yes. But the point is the reference from “The Hitchhiker’s Guide to the Galaxy”.

The answer to life the universe and everything = 42

You may already know that, but just in case I’d thought I’d point it out. It’s humor. Like the Minitest method call :i_suck_and_my_tests_are_order_dependent!

5 Likes

My Rails tip. For ActiveRecord when you want to get all records that have a matching reference from another table; use select to retrieve them.

User.where(id: Contact.select(:user_id))

This will allow one SQL query to be made rather than two had you used :pluck or :ids

See my post Rails: Don’t “pluck” Unnecessarily

8 Likes

If you have //=require_tree in your applications.js, bear in mind that it will load files in alphabetical order. So if you have grid.js, which depends on workspace.js, the dependency may fail as grid will be loaded before workspace. There are a few ways of getting round that, probably the easiest one is to prefix dependent file with numerals, i.e. 01_workspace.js, 02_grid.js. thus ensuring the right load order.

8 Likes

I try to avoid require_tree if I can help it… if only for the benefit of being more explicit about my load ordering.

2 Likes

I don’t think you need to explicitly add to autoload_paths in Rails 4, as long as the folder is under app/.

This is also a fantastic tip that I wish more Rails devs took advantage of. In our projects, we create directories and classes for all manner of abstractions. Makes life so much easier.

3 Likes

You don’t need a gem for everything. Dependencies have a cost.

3 Likes

Totally agree about using too many gems - I try not to if at all possible.

Thanks for great info. how about specifying our own order by removing //require_tree?

3 Likes

Recently In our Ruby world .freeze is the talk. matz proposed to enable freeze on string literal by default in Ruby 3.0 but it may break some gem and libraries, how about practicing that in our daily ruby work to avoid unwanted migration problem.

4 Likes

Yes that would work too, then you’d have to explicitly require each file in your manifest. If you only have a few JS files, this may be the safest approach because you then have control over the load order.

Brandon Hilkert wrote a good article on how to organise Javascript in your Rails App. Worth a read.

2 Likes

:+1::+1::+1::+1::+1::+1:

Here is one way to execute raw sql from the rails console:

sql_string = "select name, count(name) from people group by name having count(name) > 1"

r=ActiveRecord::Base.connection.execute(sql)

r.values 
#=> [["Jim", "2"], ["Connie", "2"]]

I use r.values to access the results since I’m using Postgres. I think I read somewhere the results access might be a bit different depending on your database.

2 Likes

If it about dependency
I put //= require ../parent_dependency to ensure the dependency get loaded
Otherwise I don’t know why load order matters :stuck_out_tongue:

1 Like

Learn Arel for complex queries. It composes better then Strings and can even be combined with AR scopes.

3 Likes

I’ll like to add to that that most times you don’t need Arel, but just ActiveRecord. It’s capable of more than you think.

1 Like

That’s true. I think we’re both saying “You probably don’t need strings of SQL in your code”

2 Likes

Use Lotus and Sinatra instead! :

(let the flames begin) :fire_engine: :running:

1 Like