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
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!
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.
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.
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.
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.
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.