2013-10-14 02:36:58 +00:00
|
|
|
---
|
|
|
|
title: Rails tricks
|
2015-11-24 05:02:17 +00:00
|
|
|
category: Rails
|
2023-03-14 04:40:29 +00:00
|
|
|
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
|
|
|
|
2023-03-14 04:40:29 +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
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### Partial locals
|
2014-05-16 11:04:02 +00:00
|
|
|
|
|
|
|
<%= render 'article', full: true %>
|
|
|
|
<%= render 'article' %>
|
|
|
|
|
|
|
|
<% if local_assigns[:full] %>
|
|
|
|
...
|
|
|
|
<% end %>
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### HTML in i18n
|
2014-05-16 11:04:02 +00:00
|
|
|
|
|
|
|
en:
|
|
|
|
read_more_html: "read <b>more</b>..."
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### 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
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### Rails updating
|
2014-05-16 11:04:02 +00:00
|
|
|
|
|
|
|
rake rails:update
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### Distinct pluck
|
2014-05-16 11:04:02 +00:00
|
|
|
|
|
|
|
Article.distinct.pluck('author')
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### Relation#merge
|
2014-05-16 11:04:02 +00:00
|
|
|
|
|
|
|
scope :with_drafts, -> {
|
|
|
|
uniq.joins(:articles).merge(Article.draft)
|
|
|
|
}
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### Order
|
2014-05-16 11:04:02 +00:00
|
|
|
|
|
|
|
scope :recent, -> { order created_at: :desc }
|
|
|
|
|
2023-03-14 04:40:29 +00:00
|
|
|
### 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
|