Share a Rails Tip Weekly!

“This thread is for the ZShell specifically”… errr… Rails :stuck_out_tongue:

1 Like

I’ve found that the updated sass collection of gems, which have in their instructions to change application.css to application.css.scss, don’t recognize just ‘application’ as a parameter for a scss file for stylesheet_link_tag. You need to change:

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>

to this:

<%= stylesheet_link_tag 'application.css.scss', media: 'all', 'data-turbolinks-track' => true %>

This will allow you to work with the latest gems.

2 Likes

just getting my own back :laughing:

2 Likes

Don’t add your own helper methods that are not used frequently and globally
I am creating another site (different layout, view components, displays)
The only thing that is so difficult to extract is helper methods

Luckily I am already using cells which can be copied and modified (and proper namespaced)

2 Likes

One thing I ran into with ActiveRecord: You can’t do an SQL UNION operation. In which case I believe you’ll need Arel or raw SQL.

2 Likes

Had to do this today, if you want to route /hello to /public/hello.html in Rails 4, in routes.rb do:

get "/hello", :to => redirect('/hello.html')
3 Likes

Using “require” on files inside autoload path will screw up things sometimes…
e.g. require Rails.root.join("app", "model", "something")
Using require_relative will also do the same

I think (and not proved) the auto-loading loads the file again and make the code executed twice
I have this guess since I am using Contract in many model classes
and I got “duplicate contract” error if I require one of those model files

3 Likes

Indenting heredocs for readability - use strip_heredoc from Active Support Core Extensions:

if options[:usage]
  puts <<-USAGE.strip_heredoc
    This command does such and such.
 
    Supported options are:
      -h         This message
      ...
  USAGE
end
2 Likes

You can use controller_name and action_name to get the name of the controller and the name of the controller action, respectively, in your Rails view:

- if action_name == "show"
  = javascript_include_tag "my_custom_javascript"
1 Like

Please don’t! :scream:
There is no way to know the purpose/reason of that expression

At least it should be:

- if should_render_optional_sections # The name depends on what to do
  -# Do whatever needs to be done, like rendering

action_name == "show" is the implementation detail, not the abstraction

Also it might still be unclear, so add comment when appropriate

Edit 1: Fix my bad English grammar mistakes

4 Likes
rake tmp:clear

Use this to clear out the tmp directory - I was working locally and about 13 MB had accumulated…I didn’t realize how much cruft accumulated there!

Use the railroady gem to generate graphical diagrams of your database relationships.

2 Likes

Nice - looks like an interesting gem!

Long time no share

  • After creating a migration, test the migration by
    • running the migration (db:migrate or db:migrate:up VERSION=SOMETHING)
    • rolling the migration back (db:rollback or db:migrate:down VERSION=SOMETHING)
    • running the migration again

I have seen some migrations failed to handle the rollback scenario

3 Likes

Awesome! I usually do rollbacks by steps.

rake db:rollback STEP=1
2 Likes

For advanced queries in Rails using Arel is really handy

But when doing so you should know that the Rails team considers Arel as an “internal, private API” and therefore they don’t like it when you depend on it. Arel will likely break compatibility between Rails versions.

For example the Arel method eq_any(), which takes a collection of possible records, changed from Rails 4.1 to Rails 4.2 and broke backwards compatibility. It no longer works with an empty collection. E.G. eq_any() fails. This is bad as you may do a query that bring back an empty result and now the method blows up.

To save yourself the headache of breaking changes either don’t use Arel, or don’t upgrade rails, or switch to an external database tool such as Sequel

If I had known what I know now about Rails’ team attitude towards Arel use, and it having breaking changes, I would have not used Arel or ActiveRecord but used Sequel instead.

1 Like

Configure your timezone in application.rb

config.time_zone = 'London'

I just spent a few hours wondering why the created_at time was different to Time.now (and Time.now different to Time.current).

Wish there was just a simple way to make Rails use the server time (the hacks on the web don’t seem to work).

2 Likes

This might help you config.active_record.default_timezone

kindly let me know if this resolved your problem… :slight_smile:

Which bit @ferdinandrosario - nothing I tried made any difference. Even now the created_at time is different to Time.now but Rails keeps track of it and modifies how you interact with it in accordance with the set time zone.

Put one action in one controller class
I learn that from Lotus/Hanami