はてなブックマークアプリ

サクサク読めて、
アプリ限定の機能も多数!

アプリで開く

はてなブックマーク

  • はてなブックマークって?
  • アプリ・拡張の紹介
  • ユーザー登録
  • ログイン
  • Hatena

はてなブックマーク

トップへ戻る

  • 総合
    • 人気
    • 新着
    • IT
    • 最新ガジェット
    • 自然科学
    • 経済・金融
    • おもしろ
    • マンガ
    • ゲーム
    • はてなブログ(総合)
  • 一般
    • 人気
    • 新着
    • 社会ニュース
    • 地域
    • 国際
    • 天気
    • グルメ
    • 映画・音楽
    • スポーツ
    • はてな匿名ダイアリー
    • はてなブログ(一般)
  • 世の中
    • 人気
    • 新着
    • 新型コロナウイルス
    • 働き方
    • 生き方
    • 地域
    • 医療・ヘルス
    • 教育
    • はてな匿名ダイアリー
    • はてなブログ(世の中)
  • 政治と経済
    • 人気
    • 新着
    • 政治
    • 経済・金融
    • 企業
    • 仕事・就職
    • マーケット
    • 国際
    • はてなブログ(政治と経済)
  • 暮らし
    • 人気
    • 新着
    • カルチャー・ライフスタイル
    • ファッション
    • 運動・エクササイズ
    • 結婚・子育て
    • 住まい
    • グルメ
    • 相続
    • はてなブログ(暮らし)
    • 掃除・整理整頓
    • 雑貨
    • 買ってよかったもの
    • 旅行
    • アウトドア
    • 趣味
  • 学び
    • 人気
    • 新着
    • 人文科学
    • 社会科学
    • 自然科学
    • 語学
    • ビジネス・経営学
    • デザイン
    • 法律
    • 本・書評
    • 将棋・囲碁
    • はてなブログ(学び)
  • テクノロジー
    • 人気
    • 新着
    • IT
    • セキュリティ技術
    • はてなブログ(テクノロジー)
    • AI・機械学習
    • プログラミング
    • エンジニア
  • おもしろ
    • 人気
    • 新着
    • まとめ
    • ネタ
    • おもしろ
    • これはすごい
    • かわいい
    • 雑学
    • 癒やし
    • はてなブログ(おもしろ)
  • エンタメ
    • 人気
    • 新着
    • スポーツ
    • 映画
    • 音楽
    • アイドル
    • 芸能
    • お笑い
    • サッカー
    • 話題の動画
    • はてなブログ(エンタメ)
  • アニメとゲーム
    • 人気
    • 新着
    • マンガ
    • Webマンガ
    • ゲーム
    • 任天堂
    • PlayStation
    • アニメ
    • バーチャルYouTuber
    • オタクカルチャー
    • はてなブログ(アニメとゲーム)
    • はてなブログ(ゲーム)
  • おすすめ

    新内閣発足

『Rails Best Practices - Rails Best Practices』

  • 人気
  • 新着
  • すべて
  • Rails Best Practices - Use Time.zone.now instead of Time.now

    4 users

    rails-bestpractices.com

    Before Using default Ruby Time, Date and DateTime classes will not show times in the time zone specified by config.time_zone in application.rb. Time.zone = "Alaska" Time.now Date.today These show the local time, not the time in Alaska (unless you're already in Alaska). Refactor You should instead use ActiveSupport methods of Time.zone to pickup the Rails time zone. Time.zone.now Time.zone.today Th

    • テクノロジー
    • 2015/01/18 13:59
    • Rails Best Practices | Use query attribute

      6 users

      rails-bestpractices.com

      Do you always check if ActiveRecord's attributes exist or not by nil?, blank? or present? ? Don't do that again, rails provides a cleaner way by query attribute Bad Smell <% if @user.login.blank? %> <%= link_to 'login', new_session_path %> <% end %> <% if @user.login.present? %> <%= @user.login %> <% end %> It's not bad, but rails provides a cleaner way, we should use query attributes to make code

      • テクノロジー
      • 2014/07/16 16:04
      • Rails
      • Rails Best Practices | Test STDIN / STDOUT in Rspec

        4 users

        rails-bestpractices.com

        I would try mapping $stdout and $stdin to some temp variables, assigning a File object to $stdout and $stdin and then restoring originals: original_stdout, original_stdin = $stdout, $stdin $stdout, $stdin = File.new('stdout.log', 'w+'), File.new('stdin.log', 'w+') ... #Do operations with my class ... #Restore original stdout and stdin and close opened files $stdout.close ; $stdin.close $stdout, $s

        • 学び
        • 2013/04/28 01:16
        • ruby
        • coding
        • testing
        • rails
        • Rails Best Practices | Clever enums in rails

          9 users

          rails-bestpractices.com

          After many years of rails developing I have finally found satisfying solution to implement enums in rails. Before class Photo < ActiveRecord::Base STATUSES = ['queued', 'new', 'changed', 'removed', 'ready'] def change_status self.status = 'changed' end end In this example we have a list of statuses stored in db as strings. Changing status requires developer to find STATUSES Array and manually type

          • 暮らし
          • 2013/04/19 11:06
          • enum
          • rails
          • ruby
          • Rails Best Practices | Fetch current user in models

            6 users

            rails-bestpractices.com

            I don't remember how many times I need to fetch current user in models, such as audit log. Here is a flexible way to set the current user in and fetch the current user from User model. I don't remember how many times I need to fetch the current user in models, for example, I want to log who creates, updates or destroys a post in the Audit model. There is no default way to fetch the current user in

            • 世の中
            • 2012/07/26 16:26
            • Rails
            • *programming
            • ruby
            • Rails Best Practices | use after_commit

              5 users

              rails-bestpractices.com

              Most developers use AR callbacks after_create/after_update/after_destroy to generate background job, expire cache, etc., but they don't realize these callbacks are still wrapped in database transaction, they probably got unexpected errors on production servers. A relational database, like mysql, provides transactions to wrap several operations in one unit, make them all pass or all fail. All isol

              • 世の中
              • 2012/05/04 22:08
              • rails
              • ruby
              • *programming
              • Rails Best Practices | DRY your database.yml

                3 users

                rails-bestpractices.com

                Use anchors (&) and references (*) to merge options allowing your database.yml to not be so repetitive. Obviously you can organize in a way that makes the most sense to your organization but the following is an example where the database and test environments share some config and the production and staging environment share some config. So rather than repeating them you we just tag the first one

                • 学び
                • 2012/04/16 16:15
                • *programming
                • rails
                • ruby
                • Rails Best Practices | Protect mass assignment

                  3 users

                  rails-bestpractices.com

                  Rails mass assignment feature is really useful, but it may be a security issue, it allows an attacker to set any models' attributes you may not expect. To avoid this, we should add attr_accessbile or attr_protected to all models. Last weekend github is hacked because of mass assignment issue, actually it's not rails fault, it's a "junior" develop forgot to add attr_accessible or attr_protected to

                  • 学び
                  • 2012/03/06 23:04
                  • Ruby on Rails
                  • Rails Best Practices | Not use time_ago_in_words

                    4 users

                    rails-bestpractices.com

                    It's very common for a rails developer to use time_ago_in_words to display time like "5 minutes ago", but it's too expensive to calculate the time in server side, you should utilize client cpu to calculate the time ago. Rails provides a helper method time_ago_in_words to display the distance between one time and now, like "5 minute ago", it's very useful. Before <%= times_ago_in_words(comment.cre

                    • 学び
                    • 2012/02/20 01:20
                    • Rails Best Practices | Generate polymorphic url

                      3 users

                      rails-bestpractices.com

                      If you want to generate different urls according to different objects, you should use the polymorphic_path/polymorphic_url to simplify the url generation. Imagine that we have three models, Post, News and Comment. It's common that a post has many comments and a news has many comments, so we define them as class Post < ActiveRecord::Base has_many :comments end class News < ActiveRecord::Base has_ma

                      • 暮らし
                      • 2012/01/24 19:34
                      • rails
                      • Rails Best Practices | Use cells to abstract view widgets

                        9 users

                        rails-bestpractices.com

                        Rails developers always pay more attentions on models and controllers refactoring, they don't take care about views modularization, that makes view codes most difficult to maintain. Here I recommend you to use cells gem to write more reuseable, testable and cacheable view codes. Rails developers have a strong idea to write beautiful and high quality ruby codes for models and controllers, they alwa

                        • 世の中
                        • 2011/11/07 10:30
                        • rails
                        • ruby
                        • あとで読む
                        • Rails Best Practices | Use I18n.localize for date/time formating

                          3 users

                          rails-bestpractices.com

                          For reliable formatting of a date/time string in the desired language, use I18n.localise, Time#strftime can cause u unnecessary headache. On different machines, Time#strftime can yield different results, and there is no reliable way to ensure u get the string with the desired language, even after setting the environment variable $LANG, and even recompiling ruby under the desired locale setting. Us

                          • 暮らし
                          • 2011/10/16 07:43
                          • Rails Best Practices | Active Record Query Interface Optimization

                            3 users

                            rails-bestpractices.com

                            Using the select parameter in Active Record association your can speed up you application about 50% and more. The following example is only focused on the optimization of the association using select, so there are further optimizations for the following examples. Given the models: class Patient < ActiveRecord::Base belongs_to :physician end class Physician < ActiveRecord::Base has_many :patients e

                            • 世の中
                            • 2011/06/26 09:44
                            • rails
                            • coding
                            • performance
                            • Rails Best Practices | Replace instance variable with local variable

                              4 users

                              rails-bestpractices.com

                              In partial view, we can use the instance variable directly, but it may be confused and make it hard to reuse anywhere, because we don't know exactly which instance variable can be used, so use the local variable in partial with explicitly assignment. Bad Smell class PostsController < ApplicationController def show @post = Post.find(params[:id]) end end <%= render :partial => "sidebar" %> In this e

                              • 暮らし
                              • 2011/05/19 10:58
                              • Rails
                              • Rails Best Practices | Replace Complex Creation with Factory Method

                                3 users

                                rails-bestpractices.com

                                Sometimes you will build a complex model with params, current_user and other logics in controller, but it makes your controller too big, you should move them into model with a factory method Bad Smell class InvoicesController < ApplicationController def create @invoice = Invoice.new(params[:invoice]) @invoice.address = current_user.address @invoice.phone = current_user.phone @invoice.vip = (@invoi

                                • 世の中
                                • 2011/05/19 10:57
                                • *programming
                                • ruby
                                • Rails Best Practices | split route namespaces into different files

                                  4 users

                                  rails-bestpractices.com

                                  the routes will become complicated with the growth of your application, contain different namespaces, each with a lot of resources and custom routes, it would be better to split routes into different files according to the namespaces, which makes it easy to maintain the complicated routes. I experienced that with the growth of application, the routes becomes very complicated. The following is a si

                                  • 世の中
                                  • 2011/05/05 00:26
                                  • rails
                                  • ruby
                                  • Rails Best Practices | comment your magic codes

                                    3 users

                                    rails-bestpractices.com

                                    Ruby/Rails provides a lot of magic codes, especially for metaprogramming, they are powerful, less codes to implement more functions, but they are not intuition, you should write good comment for your magic codes. Ruby/Rails provides a lot of magic codes, especially for metaprogramming, they are powerful, less codes to implement more functions, but they are not intuition. You may quickly write the

                                    • 学び
                                    • 2010/12/20 12:11
                                    • Rails Best Practices | Simplify render in views

                                      11 users

                                      rails-bestpractices.com

                                      render is one of the often used view helpers, we can pass object, collection or local variables. From rails 2.3, more simplified syntax for render are provided. render is one of the often used view helpers, we use it to extract sub part view. We can pass object, collection or local variables to the partial views. From rails 2.3, more simplified syntax for render are provided that makes render help

                                      • 暮らし
                                      • 2010/12/05 21:02
                                      • rails
                                      • Rails Best Practices | Use memoization

                                        11 users

                                        rails-bestpractices.com

                                        Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. In rails, you can easily use memoize which is inherited from ActiveSupport::Memoizable. Memoization is an optimization technique used primarily to speed up computer programs by having function call avoid repeat th

                                        • 学び
                                        • 2010/11/23 20:36
                                        • rails
                                        • ruby
                                        • プログラミング
                                        • *あとで
                                        • tips
                                        • Rails Best Practices | DRY bundler in capistrano

                                          4 users

                                          rails-bestpractices.com

                                          There are a few posts told you how to integrate bundler into capistrano, but they are out of date now. After bundler 1.0 released, you can add only one line in capistrano to use bundler. There are a few posts told you how to integrate Bundler into capistrano, the code snippet is as follows namespace :bundler do task :create_symlink, :roles => :app do shared_dir = File.join(shared_path, 'bundle') r

                                          • 学び
                                          • 2010/09/04 13:44
                                          • Rails Best Practices | use OpenStruct when advance search

                                            3 users

                                            rails-bestpractices.com

                                            Before in view <% form_for_tag blabal do |f| %> <%= f.text_field_tag :quick, params[:search][:quick] %> <%= select_tag("country", options_for_select([["unassigned" , "0" ]] + Country.to_dropdown, region.country_id), {:name => "search[country]"} ) %> <%= f.submit "Search" %> <% end %> After in controller require 'ostruct' def index @search = OpenStruct.new(params[:search]) end in view <% form_for :

                                            • 学び
                                            • 2010/08/26 00:28
                                            • rails
                                            • form
                                            • ruby
                                            • http://rails-bestpractices.com/posts/45-use-sti-and-polymorphic-model-for-multiple-uploads

                                              4 users

                                              rails-bestpractices.com

                                              • 暮らし
                                              • 2010/08/19 00:48
                                              • rails
                                              • database
                                              • ruby
                                              • development
                                              • Rails Best Practices - Rails Best Practices

                                                113 users

                                                rails-bestpractices.com

                                                The params hash contains all the data that was submitted from a request. If you modify it, later code won't have access to it. Instead, copy the params hash and modify the copy. Read More

                                                • 世の中
                                                • 2010/07/26 03:51
                                                • rails
                                                • ruby
                                                • RoR
                                                • best_practice
                                                • rails3
                                                • Ruby on Rails
                                                • あとで
                                                • *programming
                                                • あとで読む
                                                • Rails Best Practices - Rails Best Practices

                                                  19 users

                                                  rails-bestpractices.com

                                                  The params hash contains all the data that was submitted from a request. If you modify it, later code won't have access to it. Instead, copy the params hash and modify the copy. Read More

                                                  • 世の中
                                                  • 2010/07/26 03:51
                                                  • Rails

                                                  このページはまだ
                                                  ブックマークされていません

                                                  このページを最初にブックマークしてみませんか?

                                                  『Rails Best Practices - Rails Best Practices』の新着エントリーを見る

                                                  キーボードショートカット一覧

                                                  j次のブックマーク

                                                  k前のブックマーク

                                                  lあとで読む

                                                  eコメント一覧を開く

                                                  oページを開く

                                                  はてなブックマーク

                                                  • 総合
                                                  • 一般
                                                  • 世の中
                                                  • 政治と経済
                                                  • 暮らし
                                                  • 学び
                                                  • テクノロジー
                                                  • エンタメ
                                                  • アニメとゲーム
                                                  • おもしろ
                                                  • アプリ・拡張機能
                                                  • 開発ブログ
                                                  • ヘルプ
                                                  • お問い合わせ
                                                  • ガイドライン
                                                  • 利用規約
                                                  • プライバシーポリシー
                                                  • 利用者情報の外部送信について
                                                  • ガイドライン
                                                  • 利用規約
                                                  • プライバシーポリシー
                                                  • 利用者情報の外部送信について

                                                  公式Twitter

                                                  • 公式アカウント
                                                  • ホットエントリー

                                                  はてなのサービス

                                                  • はてなブログ
                                                  • はてなブログPro
                                                  • 人力検索はてな
                                                  • はてなブログ タグ
                                                  • はてなニュース
                                                  • ソレドコ
                                                  • App Storeからダウンロード
                                                  • Google Playで手に入れよう
                                                  Copyright © 2005-2025 Hatena. All Rights Reserved.
                                                  設定を変更しましたx