# Share a Rails Tip Weekly!

**URL:** <https://metaruby.com/t/share-a-rails-tip-weekly/306>\
**Category:** Rails Forum\
**Tags:** rails\
**Created:** [August 20, 2015, 10:53pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306 "2015-08-20T22:53:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![treble37](https://metaruby.com/user_avatar/metaruby.com/treble37/32/884_2.png) [@treble37](https://metaruby.com/u/treble37)\
**Post date:** [August 20, 2015, 10:53pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/1 "2015-08-20T22:53:14Z")

</div>

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](http://www.quora.com/Why-is-Array-forty_two-called-the-reddit-in-Ruby-on-Rails)

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [August 21, 2015, 12:53am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/2 "2015-08-21T00:53:54Z")

</div>

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

 ![](https://metaruby.com/uploads/default/original/1X/3dbbdcc043e8bd90f87c1323b725e1eabdb9406f.png) 

Just add to autoload in `application.rb`:

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

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [August 21, 2015, 7:23am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/3 "2015-08-21T07:23:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 21, 2015, 9:00am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/4 "2015-08-21T09:00:17Z")

</div>

> [@Ohm](#):
>
> it should have been :forty\_second.

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

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [August 21, 2015, 9:04am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/5 "2015-08-21T09:04:52Z")

</div>

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](http://6ftdan.com/allyourdev/2015/05/13/rails-dont-pluck-unnecessarily/)

---

<div class="post-metadata">

**Author:** ![RedFred7](https://metaruby.com/user_avatar/metaruby.com/redfred7/32/838_2.png) [@RedFred7](https://metaruby.com/u/RedFred7)\
**Post date:** [August 21, 2015, 9:13am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/6 "2015-08-21T09:13:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [August 22, 2015, 2:11pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/7 "2015-08-22T14:11:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [August 22, 2015, 2:13pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/8 "2015-08-22T14:13:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [August 22, 2015, 2:17pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/9 "2015-08-22T14:17:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [August 22, 2015, 5:12pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/10 "2015-08-22T17:12:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ferdinandrosario](https://metaruby.com/user_avatar/metaruby.com/ferdinandrosario/32/120_2.png) [@ferdinandrosario](https://metaruby.com/u/ferdinandrosario)\
**Post date:** [August 25, 2015, 5:41am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/11 "2015-08-25T05:41:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ferdinandrosario](https://metaruby.com/user_avatar/metaruby.com/ferdinandrosario/32/120_2.png) [@ferdinandrosario](https://metaruby.com/u/ferdinandrosario)\
**Post date:** [August 25, 2015, 5:47am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/12 "2015-08-25T05:47:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RedFred7](https://metaruby.com/user_avatar/metaruby.com/redfred7/32/838_2.png) [@RedFred7](https://metaruby.com/u/RedFred7)\
**Post date:** [August 25, 2015, 2:44pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/13 "2015-08-25T14:44:29Z")

</div>

> [@ferdinandrosario](#):
>
> Thanks for great info. how about specifying our own order by removing `//require_tree`?

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](http://brandonhilkert.com/blog/organizing-javascript-in-rails-application-with-turbolinks/). Worth a read.

---

<div class="post-metadata">

**Author:** ![ferdinandrosario](https://metaruby.com/user_avatar/metaruby.com/ferdinandrosario/32/120_2.png) [@ferdinandrosario](https://metaruby.com/u/ferdinandrosario)\
**Post date:** [August 26, 2015, 7:03am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/14 "2015-08-26T07:03:18Z")

</div>

👍👍👍👍👍👍

---

<div class="post-metadata">

**Author:** ![treble37](https://metaruby.com/user_avatar/metaruby.com/treble37/32/884_2.png) [@treble37](https://metaruby.com/u/treble37)\
**Post date:** [August 26, 2015, 10:51pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/15 "2015-08-26T22:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PikachuEXE](https://metaruby.com/user_avatar/metaruby.com/pikachuexe/32/854_2.png) [@PikachuEXE](https://metaruby.com/u/PikachuEXE)\
**Post date:** [August 27, 2015, 1:10am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/16 "2015-08-27T01:10:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [August 27, 2015, 11:42am UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/17 "2015-08-27T11:42:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [August 27, 2015, 1:57pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/18 "2015-08-27T13:57:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [August 27, 2015, 2:11pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/19 "2015-08-27T14:11:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RedFred7](https://metaruby.com/user_avatar/metaruby.com/redfred7/32/838_2.png) [@RedFred7](https://metaruby.com/u/RedFred7)\
**Post date:** [August 27, 2015, 2:58pm UTC](https://metaruby.com/t/share-a-rails-tip-weekly/306/20 "2015-08-27T14:58:22Z")

</div>

Use Lotus and Sinatra instead! :

(let the flames begin) 🚒 :running:

[Next page](https://metaruby.com/t/share-a-rails-tip-weekly/306.md?page=2)
