cheatsheets/rails-tricks.md

69 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: Rails tricks
2015-11-24 05:02:17 +00:00
category: Rails
tags: [Archived]
archived: This sheet may describe practices that might be outdated.
2013-10-14 02:36:58 +00:00
---
2013-05-29 12:19:07 +00:00
### Sass source maps
2013-05-29 12:19:07 +00:00
in config/environments/development.rb:
# Source maps for Sass
config.sass.debug_info = true
config.sass.line_comments = false
2014-02-25 10:32:14 +00:00
# Don't break apart
config.assets.debug = false
2014-05-16 11:04:02 +00:00
### Partial locals
2014-05-16 11:04:02 +00:00
<%= render 'article', full: true %>
<%= render 'article' %>
<% if local_assigns[:full] %>
...
<% end %>
### HTML in i18n
2014-05-16 11:04:02 +00:00
en:
read_more_html: "read <b>more</b>..."
### Exception handling
2014-05-16 11:04:02 +00:00
# config/application.rb
config.exceptions_app = self.routes
get '/404', to: 'errors#not_found'
get '/500', to: 'errors#server_error'
class ErrorsController
def not_found
render status: :not_found
end
end
### Rails updating
2014-05-16 11:04:02 +00:00
rake rails:update
### Distinct pluck
2014-05-16 11:04:02 +00:00
Article.distinct.pluck('author')
### Relation#merge
2014-05-16 11:04:02 +00:00
scope :with_drafts, -> {
uniq.joins(:articles).merge(Article.draft)
}
### Order
2014-05-16 11:04:02 +00:00
scope :recent, -> { order created_at: :desc }
### Group by month
2014-05-16 11:04:02 +00:00
2015-12-01 05:41:43 +00:00
.group("to_char(created_at, 'YYYY-MM')")
.group("to_char(created_at, 'YYYY-MM')").count