OSDN Git Service

Regular updates
[twpd/master.git] / rails-tricks.md
1 ---
2 title: Rails tricks
3 category: Rails
4 layout: 2017/sheet
5 tags: [Archived]
6 archived: This sheet may describe practices that might be outdated.
7 ---
8
9 ### Sass source maps
10
11 in config/environments/development.rb:
12
13     # Source maps for Sass
14     config.sass.debug_info = true
15     config.sass.line_comments = false
16
17     # Don't break apart
18     config.assets.debug = false
19
20 ### Partial locals
21
22     <%= render 'article', full: true %>
23     <%= render 'article' %>
24
25     <% if local_assigns[:full] %>
26       ...
27     <% end %>
28
29 ### HTML in i18n
30
31     en:
32       read_more_html: "read <b>more</b>..."
33
34 ### Exception handling
35
36     # config/application.rb
37     config.exceptions_app = self.routes
38
39     get '/404', to: 'errors#not_found'
40     get '/500', to: 'errors#server_error'
41
42     class ErrorsController
43       def not_found
44         render status: :not_found
45       end
46      end
47
48 ### Rails updating
49
50     rake rails:update
51
52 ### Distinct pluck
53
54     Article.distinct.pluck('author')
55
56 ### Relation#merge
57
58     scope :with_drafts, -> {
59       uniq.joins(:articles).merge(Article.draft)
60     }
61
62 ### Order
63
64     scope :recent, -> { order created_at: :desc }
65
66 ### Group by month
67
68     .group("to_char(created_at, 'YYYY-MM')")
69     .group("to_char(created_at, 'YYYY-MM')").count