OSDN Git Service

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