<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Rails on 答えを知りたい</title><link>https://hack.ironsand.net/tags/rails/</link><description>Recent content in Rails on 答えを知りたい</description><generator>Hugo -- 0.147.9</generator><language>en-US</language><lastBuildDate>Thu, 27 Oct 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://hack.ironsand.net/tags/rails/index.xml" rel="self" type="application/rss+xml"/><item><title>letter_openerをWSL環境で使う方法</title><link>https://hack.ironsand.net/2022/how-to-use-letter-opener-in-wsl/</link><pubDate>Thu, 27 Oct 2022 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2022/how-to-use-letter-opener-in-wsl/</guid><description>&lt;p>メールのプレビューをブラウザ上で表示できるGEM &lt;code>open_letter&lt;/code>ですが、デフォルトのWSL環境のUbuntuでは&lt;code>launchy&lt;/code>がエラーを吐いて終了してしまいます。少し設定をしておけば問題なく使えるのですが、毎回その設定方法を忘れてしまうのでメモっておきます。&lt;/p></description></item><item><title>Object#present?や#blank?でinvalid Byte Sequence in UTF 8になる時の対処法</title><link>https://hack.ironsand.net/2022/present%E3%82%84blank%E3%81%A7invalid-byte-sequence-in-utf-8%E3%81%AB%E3%81%AA%E3%82%8B%E6%99%82%E3%81%AE%E5%AF%BE%E5%87%A6%E6%B3%95/</link><pubDate>Fri, 17 Jun 2022 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2022/present%E3%82%84blank%E3%81%A7invalid-byte-sequence-in-utf-8%E3%81%AB%E3%81%AA%E3%82%8B%E6%99%82%E3%81%AE%E5%AF%BE%E5%87%A6%E6%B3%95/</guid><description>&lt;p>Rails で&lt;code>Object#present?&lt;/code>や&lt;code>Object#blank?&lt;/code>で&lt;code>ArgumentError: invalid byte sequence in UTF-8&lt;/code>が出て少し困ったのでその原因と対処法を残しておきます。&lt;/p></description></item><item><title>PostgresqlのCardinalityViolationエラーの解決方法</title><link>https://hack.ironsand.net/2022/postgresql%E3%81%AEcardinalityviolation%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95/</link><pubDate>Tue, 07 Jun 2022 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2022/postgresql%E3%81%AEcardinalityviolation%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95/</guid><description>&lt;p>Rails6で実装された&lt;code>ActiveRecord#upsert_all&lt;/code>でバルクアップデートをしようとすると&lt;/p>
&lt;blockquote>
&lt;p>ActiveRecord::StatementInvalid: PG::CardinalityViolation: ERROR: ON CONFLICT DO UPDATE コマンドは行に再度影響を与えることはできません
HINT: 同じコマンドでの挿入候補の行が同じ制約値を持つことがないようにしてください&lt;/p>&lt;/blockquote>
&lt;p>とエラーが出たけど&lt;code>CardinalityViolation&lt;/code>の意味がわからなかったので調べてみました。&lt;/p></description></item><item><title>Railsの環境によってSeleniumで使うブラウザを変える</title><link>https://hack.ironsand.net/2021/rails%E3%81%AE%E7%92%B0%E5%A2%83%E3%81%AB%E3%82%88%E3%81%A3%E3%81%A6selenium%E3%81%A7%E4%BD%BF%E3%81%86%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E3%82%92%E5%A4%89%E3%81%88%E3%82%8B/</link><pubDate>Sat, 22 May 2021 13:38:22 +0900</pubDate><guid>https://hack.ironsand.net/2021/rails%E3%81%AE%E7%92%B0%E5%A2%83%E3%81%AB%E3%82%88%E3%81%A3%E3%81%A6selenium%E3%81%A7%E4%BD%BF%E3%81%86%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E3%82%92%E5%A4%89%E3%81%88%E3%82%8B/</guid><description>&lt;p>スクレイピングをする時に使うブラウザをRailsの開発環境と本番環境で切り替えたかったので方法を記録しておきます。&lt;/p></description></item><item><title>has_oneでhas_manyの最新のレコードを取得する方法</title><link>https://hack.ironsand.net/2021/has_one%E3%81%A7has_many%E3%81%AE%E6%9C%80%E6%96%B0%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/</link><pubDate>Fri, 05 Feb 2021 19:27:58 +0900</pubDate><guid>https://hack.ironsand.net/2021/has_one%E3%81%A7has_many%E3%81%AE%E6%9C%80%E6%96%B0%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/</guid><description>&lt;p>たとえば&lt;code>User&lt;/code>モデルが&lt;code>has_many&lt;/code>で&lt;code>ArtWork&lt;/code>を持っているときに、最新の&lt;code>ArtWork&lt;/code>を&lt;code>has_one&lt;/code>でも持ちたい場合の実装方法を解説します。&lt;/p></description></item><item><title>WebStormにあってRubyMineに存在しない機能</title><link>https://hack.ironsand.net/2019/functions-difference-between-webstorm-and-rubymine/</link><pubDate>Sun, 27 Oct 2019 15:42:54 +0900</pubDate><guid>https://hack.ironsand.net/2019/functions-difference-between-webstorm-and-rubymine/</guid><description>&lt;p>現在Railsの開発にRubyMineを使っているんだけど、Nuxtなどを触るためにJS専用のWebStormを買うべきか迷ってたので機能の違いを調べてみた。&lt;/p></description></item><item><title>spring が原因で rails server が動かない時の対処法</title><link>https://hack.ironsand.net/2019/howto-solve-rails-server-error-caused-by-spring/</link><pubDate>Tue, 21 May 2019 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2019/howto-solve-rails-server-error-caused-by-spring/</guid><description>&lt;p>&lt;code>undefined method 'application' for Rails:Module&lt;/code>というエラーが出るので対策を調べました。springが原因だったようです。&lt;/p></description></item><item><title>simple_formatを適用させるとauto_linkの target: ‘_blank’が効かない</title><link>https://hack.ironsand.net/2017/auto_link-target_blank-does-not-work-with-simple_format/</link><pubDate>Wed, 01 Nov 2017 14:07:45 +0000</pubDate><guid>https://hack.ironsand.net/2017/auto_link-target_blank-does-not-work-with-simple_format/</guid><description>&lt;h3 id="状態">状態&lt;/h3>
&lt;p>こういうふうに書いたら&lt;code>target: '_blank'&lt;/code>が効かなくて困った。&lt;/p>
&lt;pre>&lt;code>simple_format(auto_link(text, :target =&amp;gt; &amp;quot;_blank&amp;quot; ))
&lt;/code>&lt;/pre>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>&lt;code>simple_format&lt;/code>は&lt;code>a&lt;/code>タグの&lt;code>target&lt;/code>属性を取り除いてしまうので順番を入れ替えて、こう書くとOK。&lt;/p>
&lt;pre>&lt;code>auto_link(simple_format(text), :target =&amp;gt; &amp;quot;_blank&amp;quot; )
&lt;/code>&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="https://github.com/tenderlove/rails_autolink/issues/20#issuecomment-24818734">https://github.com/tenderlove/rails_autolink/issues/20#issuecomment-24818734&lt;/a>&lt;/p></description></item><item><title>複数のカラムの値が重複するオブジェクトのIDを取得する</title><link>https://hack.ironsand.net/2016/%E8%A4%87%E6%95%B0%E3%81%AE%E3%82%AB%E3%83%A9%E3%83%A0%E3%81%AE%E5%80%A4%E3%81%8C%E9%87%8D%E8%A4%87%E3%81%99%E3%82%8B%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AEid%E3%82%92%E5%8F%96/</link><pubDate>Wed, 30 Mar 2016 02:32:30 +0000</pubDate><guid>https://hack.ironsand.net/2016/%E8%A4%87%E6%95%B0%E3%81%AE%E3%82%AB%E3%83%A9%E3%83%A0%E3%81%AE%E5%80%A4%E3%81%8C%E9%87%8D%E8%A4%87%E3%81%99%E3%82%8B%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AEid%E3%82%92%E5%8F%96/</guid><description>&lt;p>複数カラムの値が重複してるオブジェクトの情報を取るために&lt;/p>
&lt;pre class="lang:ruby decode:true " >posts = Post.select(:title, :user_id, :date).group(:title, :user_id, :date).having("count(*) &amp;gt; 1").all&lt;/pre>
&lt;p>とするとこんな感じの&lt;code>id&lt;/code>が&lt;code>nil&lt;/code>の配列が返ってくるので&lt;/p>
&lt;pre class="lang:ruby decode:true " >:id =&amp;gt; nil,
:user_id =&amp;gt; 1345,
:title =&amp;gt; 'foo',
:date =&amp;gt; Tue, 01 Sep 2015&lt;/pre>
&lt;pre class="lang:ruby decode:true " >posts.each do |post|
duplicated_posts = Post.where(user_id: post.user_id, title: post.title, date: post.date)
# なんか処理する
end&lt;/pre>
&lt;p>な感じで適当に処理しましょう。&lt;/p>
&lt;h3 id="追記">追記&lt;/h3>
&lt;p>こちらのほうがスッキリ書けますね。&lt;/p>
&lt;pre class="lang:ruby decode:true " >posts.each do |post|
duplicated_posts = Post.where(post.attributes.except('id'))
# なんか処理する
end&lt;/pre></description></item><item><title>Hashのキーをリネームする</title><link>https://hack.ironsand.net/2016/hash%E3%81%AE%E3%82%AD%E3%83%BC%E3%82%92%E3%83%AA%E3%83%8D%E3%83%BC%E3%83%A0%E3%81%99%E3%82%8B/</link><pubDate>Mon, 15 Feb 2016 06:08:20 +0000</pubDate><guid>https://hack.ironsand.net/2016/hash%E3%81%AE%E3%82%AD%E3%83%BC%E3%82%92%E3%83%AA%E3%83%8D%E3%83%BC%E3%83%A0%E3%81%99%E3%82%8B/</guid><description>&lt;p>Hashのキーをリネームしたかったけどデフォルトのメソッドはないようなので書いた&lt;/p>
&lt;pre class="lang:ruby decode:true " >class Hash
def rename_key(old:, new:)
return unless has_key?(old)
return if has_key?(new)
self[new] = self.delete(old)
self
end
end&lt;/pre>
&lt;h3 id="使い方">使い方&lt;/h3>
&lt;pre class="lang:ruby decode:true " >h = {a: 1, b: 2}
{
:a =&amp;gt; 1,
:b =&amp;gt; 2
}
[92] pry(main)&amp;gt; h.rename_key(old: :a, new: :c)
{
:b =&amp;gt; 2,
:c =&amp;gt; 1
}&lt;/pre>
&lt;p>変更前のキーが存在しない時と、変更後のキーが存在する時は何もしない。&lt;/p>
&lt;p>実際の入れ替え処理は&lt;code>self[new] = self.delete(old)&lt;/code>だけで終わってます。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="http://kmckelvin.com/blog/2014/06/renaming-items-in-a-ruby-hash">http://kmckelvin.com/blog/2014/06/renaming-items-in-a-ruby-hash&lt;/a>&lt;/p></description></item><item><title>rake task の多重起動を禁止する排他処理を行う</title><link>https://hack.ironsand.net/2015/rake-task-%E3%81%AE%E5%A4%9A%E9%87%8D%E8%B5%B7%E5%8B%95%E3%82%92%E7%A6%81%E6%AD%A2%E3%81%99%E3%82%8B%E6%8E%92%E4%BB%96%E5%87%A6%E7%90%86%E3%82%92%E8%A1%8C%E3%81%86/</link><pubDate>Thu, 13 Aug 2015 08:13:13 +0000</pubDate><guid>https://hack.ironsand.net/2015/rake-task-%E3%81%AE%E5%A4%9A%E9%87%8D%E8%B5%B7%E5%8B%95%E3%82%92%E7%A6%81%E6%AD%A2%E3%81%99%E3%82%8B%E6%8E%92%E4%BB%96%E5%87%A6%E7%90%86%E3%82%92%E8%A1%8C%E3%81%86/</guid><description>&lt;p>rake task で長時間起動するため排他処理をしておかないと複数起動してしまう物があったので多重起動を禁止にしました。&lt;/p>
&lt;pre class="lang:ruby decode:true " >namespace :foo do
task :update_all =&amp;gt; :environment do |task|
process_count = `ps -ef | grep #{task.name} | grep -v grep | wc -l`.to_i
next if process_count &amp;gt; 1
end
end&lt;/pre>
&lt;p>でOK。呼び出し部分をメソッド化とかしたかったけど、上手くいかなかったのでとりあえずこれで。タスクのブロック引数として&lt;code>task&lt;/code>をちゃんと書いておかないと&lt;code>task.name&lt;/code>が使えないので注意しましょう。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ShellScript – Rakeタスク実行時、プロセス起動数を確認して排他制御する – Qiita][1]&lt;/p></description></item><item><title>rake task を途中で抜けるときはreturnじゃなくてnext</title><link>https://hack.ironsand.net/2015/use-next-instead-of-return-go-out-from-rake-task/</link><pubDate>Thu, 13 Aug 2015 07:37:22 +0000</pubDate><guid>https://hack.ironsand.net/2015/use-next-instead-of-return-go-out-from-rake-task/</guid><description>&lt;pre class="lang:ruby decode:true " >task :do_something do
return if some_condition?
do_job
end
&lt;/pre>
&lt;p>というように条件に合致しない時に &lt;code>return&lt;/code>を使って処理を抜けようとするとこんなエラーになります。&lt;/p>
&lt;pre class="lang:default decode:true " >LocalJumpError: unexpected return&lt;/pre>
&lt;p>これは rake task がメソッドではなくブロックだから起きるので &lt;code>next&lt;/code> を使ってやりましょう。それで抜けれます。&lt;/p>
&lt;pre class="lang:ruby decode:true " >task :do_something do
next if some_condition?
do_job
end
&lt;/pre>
&lt;p>ちなみに&lt;code>next&lt;/code>は気持ち悪いんのでせめて&lt;code>break&lt;/code>を使いたいと思ったんですが、それだと&lt;/p>
&lt;pre class="lang:default decode:true " >LocalJumpError: break from proc-closure
&lt;/pre>
&lt;p>になってしまいます。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – How do I return early from a rake task? – Stack Overflow][1]&lt;/p></description></item><item><title>既にインデックスをつけているカラムにUnique制限をつける</title><link>https://hack.ironsand.net/2015/add-unique-constraint-to-a-already-indexed-column/</link><pubDate>Mon, 02 Feb 2015 08:18:21 +0000</pubDate><guid>https://hack.ironsand.net/2015/add-unique-constraint-to-a-already-indexed-column/</guid><description>&lt;p>Railsで既に&lt;code>index&lt;/code>をつけているカラムに&lt;code>unique&lt;/code>制限をもうけるには、ユニーク制限だけを付加することができないので一旦インデックス自体を削除してから新規に unique な index を追加します。&lt;/p>
&lt;pre>&lt;code>def change
remove_index :editabilities, [:user_id, :list_id]
add_index :editabilities, [:user_id, :list_id], unique: true
end
&lt;/code>&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – How to add &lt;code>unique&lt;/code> constraint to already existing index by migration – Stack Overflow][1]&lt;/p></description></item><item><title>ActiveSupport::Inflectorで定義した頭字語が`has_many`を解釈してくれない時の解決法</title><link>https://hack.ironsand.net/2015/howto-fix-when-acronym-is-not-recognized-defined-in-activesupport-inflector/</link><pubDate>Mon, 02 Feb 2015 07:23:20 +0000</pubDate><guid>https://hack.ironsand.net/2015/howto-fix-when-acronym-is-not-recognized-defined-in-activesupport-inflector/</guid><description>&lt;p>Rubyの命名規則としてクラス名は通常は&lt;code>CamelCase&lt;/code>で記述しますが、&lt;code>HTTP&lt;/code>などの頭字語(Acronym)はそのまますべて大文字で記述します。&lt;/p>
&lt;p>Railsで頭字語のクラス名を使うときはモデル等を生成する前に設定で以下の記述を行っておくと自動的に認識してくれます。&lt;/p>
&lt;h4 id="configinitializersinflectionsrb">config/initializers/inflections.rb&lt;/h4>
&lt;pre tabindex="0">&lt;code>ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym &amp;#39;GNU&amp;#39;
end
&lt;/code>&lt;/pre>&lt;p>ただ&lt;code>has_many&lt;/code>でこのクラスを持とうとすると以下のエラーになってしまいます。&lt;/p>
&lt;blockquote>
&lt;p>LoadError: Unable to autoload constant Gnu, expected&lt;/p>&lt;/blockquote>
&lt;blockquote>
&lt;p>/Users/ironsand/dev/myproject/app/models/gnu.rb to define it&lt;/p>&lt;/blockquote>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>クラス名を明示的に指定するとOKです。&lt;/p>
&lt;pre tabindex="0">&lt;code>has_many :gnus, class_name =&amp;gt; &amp;#34;GNU&amp;#34;
&lt;/code>&lt;/pre></description></item><item><title>Railsで使うpostgresqlのスーパーユーザーを作成する</title><link>https://hack.ironsand.net/2014/create-postgres-superuser-for-rails/</link><pubDate>Fri, 25 Apr 2014 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2014/create-postgres-superuser-for-rails/</guid><description>&lt;p>RailsでPostgresqlを使うときに毎回そのためのユーザーの作成方法を忘れてしまうのでメモ。&lt;/p>
&lt;!-- more -->
&lt;h3 id="まずユーザーを作成">まずユーザーを作成&lt;/h3>
&lt;p>Windowsで&lt;code>Chocolatey&lt;/code>を使って&lt;code>postgresql&lt;/code>をインストールしてると初期パスワードは&lt;code>Postgres1234&lt;/code>になってるので、&lt;/p>
&lt;pre tabindex="0">&lt;code>$ psql.exe -U postgres
ユーザ postgres のパスワード: ←ここでパスワードを入力
psql (9.2.1)
&amp;#34;help&amp;#34; でヘルプを表示します.
postgres=# create user deployer;
CREATE ROLE
postgres=# alter user deployer with superuser;
ALTER ROLE
&lt;/code>&lt;/pre>&lt;p>これでパスワード無しの&lt;code>deployer&lt;/code>ユーザーが必要な権限付きで作成される。&lt;/p>
&lt;p>※ Linux系のOSなら&lt;code>sudo -u postgres psql&lt;/code>で&lt;code>psql&lt;/code>コマンドを使いましょう。&lt;/p>
&lt;h3 id="パスワード無しユーザーでもokにする">パスワード無しユーザーでもOKにする&lt;/h3>
&lt;p>これだけだと&lt;code>fe_sendauth: no password supplied&lt;/code>というエラーが出てしまうので&lt;/p>
&lt;p>&lt;code>C:\PostgreSQL\data\pg_hba.conf&lt;/code>を開いて&lt;/p>
&lt;pre tabindex="0">&lt;code># IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
&lt;/code>&lt;/pre>&lt;p>を&lt;/p>
&lt;pre tabindex="0">&lt;code># IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
&lt;/code>&lt;/pre>&lt;p>に書き換える。&lt;/p></description></item><item><title>WindowsのRailsでtwitter-bootstrap-railsを使う方法</title><link>https://hack.ironsand.net/2014/how-to-use-twitter-bootstrap-rails-in-windows/</link><pubDate>Sun, 20 Apr 2014 09:13:20 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-use-twitter-bootstrap-rails-in-windows/</guid><description>&lt;p>&lt;code>twitter-bootstrap-rails&lt;/code>を普通にWindowsで使おうとすると&lt;code>libv8がないからアカンで～&lt;/code>的なエラーが出てきてインストールに失敗します。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>特定のバージョンはインストールできるのでそれをインストールしましょう。&lt;/p>
&lt;p>&lt;code>gem install libv8 --version 3.11.8.0&lt;/code>&lt;/p>
&lt;p>あとは&lt;code>Gemfile&lt;/code>に&lt;code>gem 'therubyracer', platforms: :ruby&lt;/code>のコメントアウトを外して&lt;code>gem twitter-bottstrap-rails&lt;/code>を追加すれば万事OK！&lt;/p>
&lt;h3 id="追記より良い解決策">追記、より良い解決策&lt;/h3>
&lt;p>そもそも&lt;code>bootstrap-sass&lt;/code>を使えばよいのでは？&lt;/p></description></item><item><title>Rails+Angular.js で InvalidAuthenticityTokenエラーが出るときの対処法</title><link>https://hack.ironsand.net/2014/how-to-handle-invalidauthenticitytoken-error-in-rails-and-angular/</link><pubDate>Thu, 17 Apr 2014 18:37:04 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-handle-invalidauthenticitytoken-error-in-rails-and-angular/</guid><description>&lt;p>RailsのDBからGETでデータの呼び出しはできるけど、PUTで値の追加ができなくてこんなエラーに成った時の対処法&lt;/p>
&lt;pre class="lang:default decode:true " >Completed 422 Unprocessable Entity in 126ms
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:&lt;/pre>
&lt;h3 id="原因">原因&lt;/h3>
&lt;p>CSRF対策です。&lt;/p>
&lt;h3 id="ダメな対策">ダメな対策&lt;/h3>
&lt;p>モデルにこれを追加&lt;/p>
&lt;pre class="lang:ruby decode:true " >skip_before_filter :verify_authenticity_token&lt;/pre>
&lt;p>サイバーノーガード戦法です。おすすめしません。&lt;/p>
&lt;h3 id="良さそうな対策">良さそうな対策&lt;/h3>
&lt;p>gem の&lt;code>&amp;lt;a href=&amp;quot;https://github.com/xrd/ng-rails-csrf&amp;quot;&amp;gt;ns-csrf-rails&amp;lt;/a&amp;gt;&lt;/code>を使う。わからないことは全部gemにまかせてしまいましょう。&lt;/p></description></item><item><title>Deviseのユーザーを`rake db:seed`で追加する</title><link>https://hack.ironsand.net/2014/how-to-add-devise-user-by-rake-db-seed/</link><pubDate>Sun, 30 Mar 2014 21:08:01 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-add-devise-user-by-rake-db-seed/</guid><description>&lt;p>&lt;code>db/seeds.rb&lt;/code>に&lt;code>User.create&lt;/code>で&lt;code>encrypted_password&lt;/code>を与えてDeviseのユーザーを作ろうとしたら失敗した。&lt;/p>
&lt;h3 id="原因">原因&lt;/h3>
&lt;p>passwordが存在しないために&lt;code>validation&lt;/code>に引っかかってしまってる。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>&lt;code>password&lt;/code>を平文で打てば解決するらしいけど、平文のパスワードは使いたくないのでバリデーションの方を無効化してしまいましょう。&lt;/p>
&lt;p>&lt;code>User.new(email: someone@example.com).save(validate: false)&lt;/code>&lt;/p>
&lt;p>でOKです。&lt;code>encrypted_password&lt;/code>の取得方法は色々あるでしょうが、自分の場合は一度Deviseで作ってからそれを&lt;code>rails console&lt;/code>使って&lt;code>User.first.encrypted_password&lt;/code>を取ってきました。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Cannot create Devise account using rake db:seed for Rails 3.0 – Stack Overflow][1]&lt;/p></description></item><item><title>Wheneverを使うRailsプロジェクトをWindowsで触るときの注意点</title><link>https://hack.ironsand.net/2014/rails-project-with-whenever-in-windows/</link><pubDate>Fri, 28 Mar 2014 07:29:47 +0000</pubDate><guid>https://hack.ironsand.net/2014/rails-project-with-whenever-in-windows/</guid><description>&lt;p>&lt;code>/usr/bin/env: ruby.exe: そのようなファイルやディレクトリはありません&lt;/code>&lt;/p>
&lt;p>というエラーが&lt;code>whenever&lt;/code>で作ったcronのログとして出てきて困ってた。&lt;/p>
&lt;p>&lt;code>bin/rails&lt;/code>の実行権限とか&lt;code>/usr/bin/env ruby --version&lt;/code> でちゃんとrubyが動いてるかどうか確認したりとか色々。&lt;/p>
&lt;h3 id="答え">答え&lt;/h3>
&lt;p>Windowsで作ったRailsプロジェクトだったので &lt;code>ruby&amp;lt;strong&amp;gt;.exe&amp;lt;/strong&amp;gt;&lt;/code>になってた。なんてことだ…。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>&lt;code>bin/rails&lt;/code>の一行目&lt;code>usr/bin/env ruby.exe&lt;/code>の&lt;code>.exe&lt;/code>を削除しておきましょう。ふぁっく。&lt;/p></description></item><item><title>rbenv: version `2.0.0′ is not installed の対処法</title><link>https://hack.ironsand.net/2014/rbenv-version-2-0-0-is-not-installed/</link><pubDate>Thu, 20 Mar 2014 05:15:06 +0000</pubDate><guid>https://hack.ironsand.net/2014/rbenv-version-2-0-0-is-not-installed/</guid><description>&lt;p>&amp;ldquo;Everyday Rails Testing with RSpec&amp;quot;を購入して読んでると&lt;/p>
&lt;p>サンプルプロジェクトが紹介されてたので&lt;code>git clone&lt;/code>でダウンロードしてきて &lt;code>bundle install&lt;/code> をすると&lt;/p>
&lt;p>&lt;code>rbenv: version '2.0.0' is not installed&lt;/code>&lt;/p>
&lt;p>と出ちゃって困る。しかも &lt;code>ruby -v&lt;/code> と確認しようと思ったら再び&lt;/p>
&lt;p>&lt;code>rbenv: version '2.0.0' is not installed&lt;/code>&lt;/p>
&lt;p>？？？？？？&lt;/p>
&lt;p>「まさか rbenv が壊れた！？」と思って調べてたら、どうやらカレントディレクトリに&lt;code>.ruby-version&lt;/code>があるとそのバージョンの Ruby を参照しようとするために起きるらしい。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>自分の場合は&lt;code>2.0.0.p247&lt;/code>が入っているので該当のディレクトリで&lt;code>rbenv local&lt;/code>すると&lt;code>.ruby-version&lt;/code>の中身が書き換えられて上手く動くようになりました。&lt;/p>
&lt;p>Version 自体が異なる場合は上手く動かないと困るので該当のバージョンの ruby を&lt;code>rbenv install 2.x.x&lt;/code>でインストールしましょう。&lt;/p></description></item><item><title>datatimeとtimestampの違いとRailsで知っておくべきこと</title><link>https://hack.ironsand.net/2014/difference-between-datatime-and-timestamp-in-rails/</link><pubDate>Mon, 17 Mar 2014 19:56:14 +0000</pubDate><guid>https://hack.ironsand.net/2014/difference-between-datatime-and-timestamp-in-rails/</guid><description>&lt;p>何回か調べたけど未だに覚えられてないのでもう一度調べ直しました。&lt;/p>
&lt;p>確か前回調べたときは英語読むのがめんどくさいから日本語の情報を見て納得したんですが、今回はめんどくさがらずに英語圏で検索したらいつも通りStackOverflowにて大変わかりやすい答えが見つかりました。ありがたや〜。 (人´∀｀)&lt;/p>
&lt;h3 id="datatime-と-timestamp-の違い">datatime と timestamp の違い&lt;/h3>
&lt;p>&lt;code>datatime&lt;/code> が 1000年から9999年までを表現して &lt;code>timestamp&lt;/code> は &lt;code>unix timestamp&lt;/code>のフロントエンドに過ぎないので 1970年から2038年までしか扱えない。&lt;/p>
&lt;p>扱う範囲が違うので&lt;code>datatime&lt;/code>が8バイト&lt;code>timestamp&lt;/code>が4バイト使う。&lt;/p>
&lt;h3 id="rails-における扱い">Rails における扱い。&lt;/h3>
&lt;p>これが一番重要な気がするけど、Railsでは&lt;strong>どちらを指定してもデータベースには&lt;code>DATATIME&lt;/code>型で保存される&lt;/strong>。&lt;/p>
&lt;p>何故かこのことについて述べてるサイトがあんまりなかったのでこの記事を書いておきました。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date? – Stack Overflow][1]&lt;/p></description></item><item><title>Macで`gem install rmagick`がコケる時の対処法</title><link>https://hack.ironsand.net/2014/gem-install-rmagic-in-maverick/</link><pubDate>Mon, 27 Jan 2014 09:24:19 +0000</pubDate><guid>https://hack.ironsand.net/2014/gem-install-rmagic-in-maverick/</guid><description>&lt;p>画像編集をRailsでしたかったので &lt;code>ImageMagick&lt;/code> を Railsから使うgem &lt;code>rmagick&lt;/code> をインストールしようと思ったらこんなエラーが出てしまった。環境は Maverick。&lt;/p>
&lt;pre class="lang:default decode:true " >Can't install RMagick 2.13.1. Can't find the ImageMagick library or one of the dependent libraries. Check the mkmf.log file for more detailed information.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.&lt;/pre>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>最新版の &lt;code>2.13.2&lt;/code> をインストールすればOK。ちなみにライブラリのリンクを作るという方法も紹介されてましたがこっちは失敗。&lt;/p>
&lt;p>&lt;code>gem install rmagick -v '2.13.2'&lt;/code> でインストールしましょう。&lt;/p></description></item><item><title>rbenvでインストールしたgemをEmacsから使う</title><link>https://hack.ironsand.net/2014/how-to-use-gem-in-rbenv-with-emacs/</link><pubDate>Thu, 23 Jan 2014 08:04:42 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-use-gem-in-rbenv-with-emacs/</guid><description>&lt;p>rspec書いてる時にEmacsから簡単に呼び出せたら便利だと思って&lt;code>smart-compile&lt;/code>の設定をこんな風に書き換えたんだけど、&lt;/p>
&lt;pre class="lang:lisp decode:true " >(setq smart-compile-alist
(append
'(("\_spec.rb$" . "rspec %f"))
'(("\\.rb$" . "ruby %f"))
'(("\\.php$" . "php %f"))
'(("\\.coffee$" . "coffee -p %f"))
'(("\Gemfile$" . "bundle install"))
smart-compile-alist))
&lt;/pre>
&lt;p>でちゃんと&lt;code>Compile command: rspec foo_spec.rb&lt;/code>は呼び出せるものの実行すると&lt;/p>
&lt;pre class="lang:default decode:true " >-*- mode: compilation; default-directory: "~/dev/zombie/spec/lib/" -*-
Compilation started at Thu Jan 23 16:50:59
rspec zombie_spec.rb
/bin/bash: rspec: コマンドが見つかりません
Compilation exited abnormally with code 127 at Thu Jan 23 16:51:00&lt;/pre>
&lt;p>な感じでエラーになってしまう。&lt;/p>
&lt;p>どうやら rbenv の環境がEmacs側で認識されてないので rbenv で入れた gem のコマンドも使えないのが原因らしい。&lt;/p></description></item><item><title>Controllerで出る`syntax error, unexpected ‘:’, expecting keyword_end`エラー</title><link>https://hack.ironsand.net/2014/syntax-error-unexpected-expecting-keyword_end-in-controller/</link><pubDate>Mon, 13 Jan 2014 09:44:32 +0000</pubDate><guid>https://hack.ironsand.net/2014/syntax-error-unexpected-expecting-keyword_end-in-controller/</guid><description>&lt;pre class="lang:default decode:true " >yntax error, unexpected ':', expecting keyword_end&lt;/pre>
&lt;p>といつもの見慣れたようなTypoのエラーが出たのでControllerを確認したけどない。何度見てもちゃんとカッコ閉じてるし &lt;code>end&lt;/code> も使ってる。なのにエラーが消えない！&lt;/p>
&lt;h3 id="解決">解決&lt;/h3>
&lt;p>モデルのほうで &lt;code>has_many: words&lt;/code> って書いてました。 正しくは &lt;code>has_many :words&lt;/code> ですね。悲しい。こんなものに結構な時間を着いたしたのが悲しい。&lt;/p></description></item><item><title>Capyparaで `undefined method `visit’`と出る時の対処法</title><link>https://hack.ironsand.net/2014/how-to-solve-capypara-undefined-method-visit/</link><pubDate>Thu, 09 Jan 2014 08:55:19 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-solve-capypara-undefined-method-visit/</guid><description>&lt;p>「Rspecよくわかんねー。」という思いを胸に抱き続けてきましたが、やっぱりやらないとダメっぽいので使おうとしたら早速 &lt;code>undefined method 'visit'&lt;/code>と言われて困る。&lt;/p>
&lt;h3 id="原因">原因&lt;/h3>
&lt;p>このエラーの原因はCapybaraが最近のアップデートでこっそりと対象のディレクトリを &lt;code>requests&lt;/code> から &lt;code>features&lt;/code> に変えたのが原因らしいです。やめてほしいそういうの。&lt;/p>
&lt;p>しかも [Google group][1] にこっそり報告してるだけ。もっと こえを おおきく。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>&lt;code>spec_helper.rb&lt;/code> に以下をたす解決策もあるそうですが、将来的に requests じゃなくてfeatures を使うという意向ならそうしたほうが楽そうなので &lt;code>mv spec/requests/ spec/features&lt;/code> して自分は対策しました。&lt;/p>
&lt;p>ちなみに &lt;code>spec_helper.rb&lt;/code>に足す場合はこちら。&lt;/p>
&lt;pre class="lang:default decode:true " title="spec/spec_helper.rb" >require 'capybara/rails'
require 'capybara/rspec'
include Capybara::DSL&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – Capybara: undefined method 'visit' – Stack Overflow][2]&lt;/p>
&lt;p>[rails 3.2でRspecを導入したけど NameError – undefined method `visit' って出るときの対策 – どぶんけーブログ][3]&lt;/p></description></item><item><title>request.fullpathでparamを使わない</title><link>https://hack.ironsand.net/2014/request-fullpath-without-params/</link><pubDate>Sun, 05 Jan 2014 03:24:33 +0000</pubDate><guid>https://hack.ironsand.net/2014/request-fullpath-without-params/</guid><description>&lt;p>Railsのrequest.fullpath だと param 以降も受け取ってしまうので、paramの値を使わない時の方法。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>splitで邪魔なものをとる方法&lt;/p>
&lt;pre class="lang:ruby decode:true " >request.fullpath.split("?")[0]&lt;/pre>
&lt;p>&lt;code>path&lt;/code> を使う方法。こっちのほうがよさそう。&lt;/p>
&lt;pre class="lang:ruby decode:true " >request.path&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – request.fullpath with no parameters – Stack Overflow][1]&lt;/p></description></item><item><title>`config/routes.rb`内のブロックはブロックじゃない。</title><link>https://hack.ironsand.net/2014/config-routes-is-not-pure-ruby/</link><pubDate>Fri, 03 Jan 2014 06:19:02 +0000</pubDate><guid>https://hack.ironsand.net/2014/config-routes-is-not-pure-ruby/</guid><description>&lt;pre class="lang:ruby decode:true " >scope "api" do
resources :entries
end&lt;/pre>
&lt;p>このコードをワンラインで、&lt;/p>
&lt;pre class="lang:ruby decode:true " >scope "api" { resources :entries }&lt;/pre>
&lt;p>とかくと動かないので、routes.rb は DSLなのでブロックの書き方に影響が出てるようです。何でこうしてるかの詳しい理由はよくわからない。&lt;/p></description></item><item><title>Maverickで`rails new` が `atomic`のインストールでこける時の対処法</title><link>https://hack.ironsand.net/2014/fail-to-bundle-install-in-maverick/</link><pubDate>Fri, 03 Jan 2014 04:07:31 +0000</pubDate><guid>https://hack.ironsand.net/2014/fail-to-bundle-install-in-maverick/</guid><description>&lt;p>&lt;code>rails new&lt;/code> したらbundle install が &lt;code>atomic&lt;/code> の所でコケた。何か前も同じエラーに引っかかって結構検索した記憶があったんだけど今回も対処法を探すのに結構な時間がかかってしまった。&lt;/p>
&lt;p>エラーメッセージはこんなの。&lt;/p>
&lt;pre class="lang:sh decode:true " >run bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies.........
Installing rake (10.1.1)
Installing i18n (0.6.9)
Installing minitest (4.7.5)
Installing multi_json (1.8.2)
Installing atomic (1.1.14)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/ironsand/.rbenv/versions/2.0.0-p247/bin/ruby extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/ironsand/.rbenv/versions/2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/ironsand/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:430:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/ironsand/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:515:in `try_link0'
from /Users/ironsand/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:813:in `try_run'
from extconf.rb:26:in `&amp;lt;main&amp;gt;'
Gem files will remain installed in /Users/ironsand/dev/raffler/vendor/bundle/gems/atomic-1.1.14 for inspection.
Results logged to /Users/ironsand/dev/raffler/vendor/bundle/gems/atomic-1.1.14/ext/gem_make.out
An error occurred while installing atomic (1.1.14), and Bundler cannot continue.
Make sure that `gem install atomic -v '1.1.14'` succeeds before bundling.
ironsand@Air-2 dev$ gem update
&lt;/pre>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>エラーメッセージからだと全くわからないんだけれども、どうやらこれは Xcodeのバージョンをアップとか新規インストールした時に一度立ち上げて利用規約にOKと答えておかないとコンパイラーが使えずにでるらしい。&lt;/p></description></item><item><title>Zurb Foundation のトップバーの右上のメニューを消えないようにする</title><link>https://hack.ironsand.net/2014/zurb-foundation-top-bar-menu/</link><pubDate>Wed, 01 Jan 2014 14:05:29 +0000</pubDate><guid>https://hack.ironsand.net/2014/zurb-foundation-top-bar-menu/</guid><description>&lt;p>Zurb Foundationのトップバーの右上のメニューですが、デフォルトでは幅が940pxを切るとMenuに折りたたまれてしまうという結構広い設定になっています。&lt;/p>
&lt;p>もうちょっと幅が狭くても出るようにしたかったので修正しました。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>設定ファイルを以下のように書き換えます。環境はRailsです。&lt;/p>
&lt;pre class="lang:sass decode:true " title="app/assets/stylesheets/foundation_and_overrides.scss" >// $topbar-breakpoint: 940 !default; // Change to 9999px for always mobile layout
$topbar-breakpoint: emCalc(600px); &lt;/pre>
&lt;p>ちなみに[Menu]自体がでなくて少し困ったのですが&lt;code>toggle-topbar menu-icon&lt;/code> を設定してなかったのが原因だったようです。&lt;/p>
&lt;pre class="lang:xhtml decode:true " title="app/views/layouts/_header.html.erb" >&amp;lt;ul class="title-area"&amp;gt;
&amp;lt;li class="name"&amp;gt;
&amp;lt;h1&amp;gt;&amp;lt;%= link_to "Site Title", root_path %&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/h1&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class="toggle-topbar menu-icon"&amp;gt;&amp;lt;a href="#"&amp;gt;&amp;lt;span&amp;gt;Menu&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/pre></description></item><item><title>since_id parameter is invalid. と言われた時の対処法</title><link>https://hack.ironsand.net/2013/how-to-treat-since_id-parameter-is-invalid/</link><pubDate>Fri, 27 Dec 2013 19:14:15 +0000</pubDate><guid>https://hack.ironsand.net/2013/how-to-treat-since_id-parameter-is-invalid/</guid><description>&lt;p>Railscasts のTwitter の項目通りに書いてると &lt;code>since_id parameter is invalid.&lt;/code> のエラーでつまづきました。&lt;/p>
&lt;p>どうやらTwitterAPI1.1からの変更で &lt;code>since_id&lt;/code> に `` を指定できなくなったのが原因のようです。&lt;/p>
&lt;p>もちろんレコードにすでにつぶやきが入ってれば問題ないんですが初回の何も入ってない時にこけてしまうわけです。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;pre class="lang:ruby decode:true " >user.twitter.list_timeline(list_id, since_id: [maximum(:tweet_id),"1"].max)&lt;/pre>
&lt;p>と返り値に最低でも&lt;code>1&lt;/code>が入るようにしておけばOKです。文字列にしてるのは &lt;code>twitter_id&lt;/code>を文字列で保持してるからです。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[API v1.1 statuses/user_timeline since_id parameter is invalid | Twitter Developers][1]&lt;/p></description></item><item><title>attr_accessible は Rails4では使えなくなりました</title><link>https://hack.ironsand.net/2013/attr_accessible-is-no-more-available-in-rails4/</link><pubDate>Mon, 23 Dec 2013 09:33:40 +0000</pubDate><guid>https://hack.ironsand.net/2013/attr_accessible-is-no-more-available-in-rails4/</guid><description>&lt;p>&lt;code>attr_accessible&lt;/code>はRails4では使えなくなったのにユーザー管理の定番 gem &lt;code>devise&lt;/code>ではまだ使っているようなので新しい昨日の Strong parameter に書き換えておきましょう。&lt;/p>
&lt;h3 id="具体例">具体例&lt;/h3>
&lt;p>devise の設定で User モデルにこんなのが書いてると思うので&lt;/p>
&lt;pre class="lang:ruby decode:true " >attr_accessible :email, :password, :password_confirmation&lt;/pre>
&lt;p>その行を削除して下の方にこれを追加しましょう。&lt;/p>
&lt;pre class="lang:ruby decode:true " title="app/models/user.rb" >private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[attr_accessible][1]&lt;/p></description></item><item><title>OmniauthとDeviseの設定ファイルを両方同時に使うとルーティングがおかしくなる</title><link>https://hack.ironsand.net/2013/must-not-use-omniauthdevise-config-file-sametime/</link><pubDate>Mon, 23 Dec 2013 09:28:29 +0000</pubDate><guid>https://hack.ironsand.net/2013/must-not-use-omniauthdevise-config-file-sametime/</guid><description>&lt;p>何故だか理由はさっぱりわかりませんが、どうにもおかしな動きが &lt;code>route.rb&lt;/code> をいくらいじってもなおらないので色々情報を探してたら、config の &lt;code>omniauth.rb&lt;/code> を消したら動きました。&lt;/p>
&lt;p>consumer_key とかは devise.rb に書いておきましょう。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails 3 – Authentication failure : Devise + OmniAuth + Twitter – Stack Overflow][1]&lt;/p></description></item><item><title>Omniauth+DeviseでTwitterとかでログインもできるし連携もできるサンプル</title><link>https://hack.ironsand.net/2013/social-login-in-rails/</link><pubDate>Mon, 23 Dec 2013 08:39:04 +0000</pubDate><guid>https://hack.ironsand.net/2013/social-login-in-rails/</guid><description>&lt;p>&lt;a href="https://github.com/mohitjain/social-login-in-rails">https://github.com/mohitjain/social-login-in-rails&lt;/a>&lt;/p>
&lt;p>ここにあります。&lt;/p></description></item><item><title>Railsで ‘not null’ の制限をマイグレーションで取り除く方法</title><link>https://hack.ironsand.net/2013/remove-constraints-from-a-user-model-in-rails/</link><pubDate>Mon, 23 Dec 2013 05:25:01 +0000</pubDate><guid>https://hack.ironsand.net/2013/remove-constraints-from-a-user-model-in-rails/</guid><description>&lt;p>devise と omniauthを組みわせて使うと default の &lt;code>email&lt;/code> と &lt;code>encrypted_password&lt;/code> が必ず値を持たなければならない &lt;code>null =&amp;gt; false&lt;/code> の制限が邪魔だったのでMigrationで削除しようとしたけど方法がわからなかったので調べてみた。&lt;/p>
&lt;h3 id="やりかた">やりかた&lt;/h3>
&lt;pre class="lang:ruby decode:true " >class RemoveConditionFromUser &amp;lt; ActiveRecord::Migration
def change
change_column :users, :email, :string, null: true
change_column :users, :encrypted_password, :string, null: true
end
end&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Rails Migration: Remove constraint – Stack Overflow][1]&lt;/p></description></item><item><title>Rails開発してる私が使ってるgemの一覧</title><link>https://hack.ironsand.net/2013/gem-list-for-rails/</link><pubDate>Fri, 20 Dec 2013 02:10:27 +0000</pubDate><guid>https://hack.ironsand.net/2013/gem-list-for-rails/</guid><description>&lt;p>Macの環境構築をやり直したので、入れてるgemの一覧を出してみました。&lt;/p>
&lt;pre class="lang:default decode:true " title="`gem list`の出力結果" >actionmailer (4.0.0)
actionpack (4.0.0)
activemodel (4.0.0)
activerecord (4.0.0)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.0)
addressable (2.3.5)
arel (4.0.0)
atomic (1.1.14)
bigdecimal (1.2.0)
buftok (0.2.0)
builder (3.1.4)
bundler (1.3.5)
celluloid (0.15.2)
chunky_png (1.2.8)
coderay (1.1.0)
compass (0.12.2)
compass-normalize (1.4.3)
compass-recipes (0.3.0)
descendants_tracker (0.0.3)
diff-lcs (1.2.5)
equalizer (0.0.8)
erubis (2.7.0)
factory_girl (4.3.0)
factory_girl_rails (4.3.0)
faker (1.2.0)
faraday (0.8.8)
ffi (1.9.3)
formatador (0.2.4)
fssm (0.2.10)
guard (2.2.4)
guard-rspec (4.2.0)
hike (1.2.3)
http (0.5.0)
http_parser.rb (0.5.3)
i18n (0.6.5)
io-console (0.4.2)
json (1.8.1, 1.7.7)
listen (2.2.0)
lumberjack (1.0.4)
mail (2.5.4)
memoizable (0.2.0)
method_source (0.8.2)
mime-types (1.25)
minitest (4.3.2)
multi_json (1.8.0)
multipart-post (1.2.0)
polyglot (0.3.3)
pry (0.9.12.4)
psych (2.0.0)
rack (1.5.2)
rack-test (0.6.2)
rails (4.0.0)
railties (4.0.0)
rake (10.1.0, 0.9.6)
rb-fsevent (0.9.3)
rb-inotify (0.9.2)
rcodetools (0.8.5.0)
rdoc (4.0.0)
rspec (2.14.1)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
rspec-mocks (2.14.4)
rubygems-update (2.1.5)
sass (3.2.12)
simple_oauth (0.2.0)
slop (3.4.7)
spork (0.9.2)
sprockets (2.10.0)
sprockets-rails (2.0.0)
test-unit (2.0.0.0)
thor (0.18.1)
thread_safe (0.1.3)
tilt (1.4.1)
timers (1.1.0)
treetop (1.4.15)
twitter (5.1.1)
tzinfo (0.3.37)&lt;/pre>
&lt;p>なので、だいたい名前のわかるやつだけ入れることにします。わかんないのは依存関係のgemか全く使ってないgemなので必要になったらその時に入れることにします。&lt;/p></description></item><item><title>create_or_update 的なメソッドをRailsで使いたいとき</title><link>https://hack.ironsand.net/2013/create_or_update-method-in-rails/</link><pubDate>Sun, 15 Dec 2013 13:50:02 +0000</pubDate><guid>https://hack.ironsand.net/2013/create_or_update-method-in-rails/</guid><description>&lt;p>既に該当するモデルの値が存在していたら update で値を更新して、まだ存在しない場合は create で新しくモデルのインスタンスを作成したい時ありませんか？ 少なくとも自分にはありましたし、検索でここに辿り着いたあなたもきっとあると思います。&lt;/p>
&lt;p>Railsならきっと簡単な方法があると思ったので調べてみたらありました。&lt;/p>
&lt;h3 id="やり方">やり方。&lt;/h3>
&lt;p>こんな感じでOK。&lt;/p>
&lt;pre class="lang:ruby decode:true " >my_class = ClassName.find_or_initialize_by_name(name)
my_class.update_attributes(
:street_address =&amp;gt; self.street_address,
:city_name =&amp;gt; self.city_name,
:federalid =&amp;gt; self.federalid,
:state_prov_id =&amp;gt; self.state_prov_id,
:zip_code =&amp;gt; self.zip_code
)&lt;/pre>
&lt;h3 id="注意点">注意点&lt;/h3>
&lt;p>ちなみに &lt;code>create_or_update&lt;/code> は &lt;code>ActiveRecord::Callbacks&lt;/code> の private 関数として用意されてるご様子。&lt;/p>
&lt;p>[create_or_update (ActiveRecord::Callbacks) – APIdock][1]&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – create_or_update method in rails – Stack Overflow][2]&lt;/p></description></item><item><title>respond_to を ブロックを使わずに書く。</title><link>https://hack.ironsand.net/2013/respond_to_without_block_in_rails/</link><pubDate>Sun, 15 Dec 2013 09:29:01 +0000</pubDate><guid>https://hack.ironsand.net/2013/respond_to_without_block_in_rails/</guid><description>&lt;p>Ajaxの処理をRailsでするときはControllerに&lt;/p>
&lt;pre class="lang:ruby decode:true " >respond_to do |format|
format.js
end&lt;/pre>
&lt;p>と書いてたんですが、いちいちブロック引数に format を使って処理するのは無駄な気がしたので一行で書ける方法を調べてみた。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;pre class="lang:ruby decode:true " >respond_to :js&lt;/pre>
&lt;p>でOKでした。&lt;/p>
&lt;pre class="lang:ruby decode:true " >respond_to :html, :js&lt;/pre>
&lt;p>な書き方もOKなようです。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[respond_to (ActionController::MimeResponds::InstanceMethods) – APIdock][1]&lt;/p></description></item><item><title>Gemfileを開いてたらsmart-compileで`bundle install`を実行する</title><link>https://hack.ironsand.net/2013/run-bundle-install-to-gemfile-with-smart-compile-in-emacs/</link><pubDate>Sun, 15 Dec 2013 07:33:43 +0000</pubDate><guid>https://hack.ironsand.net/2013/run-bundle-install-to-gemfile-with-smart-compile-in-emacs/</guid><description>&lt;p>開いてるファイルをそのまま実行できる&lt;code>smart-compile&lt;/code>でGemfileを開いてたら&lt;code>bundle install&lt;/code>を実行したかったのでやり方を調べてみました。&lt;/p>
&lt;h3 id="設定方法">設定方法&lt;/h3>
&lt;p>どうやらsmart compile は自動的にコマンドの実行ディレクトリをファイルがある場所に移動してくれるようなので単純に &lt;code>Gemfile&lt;/code>というファイル名と&lt;code>bundle install&lt;/code>のコマンドを他のファイルと同じように紐付ければOKのようです。&lt;/p>
&lt;pre class="lang:lisp decode:true " title="~/.emacs.c/inits/30-smart-compile.el" >(global-set-key (kbd "C-c C-x") 'smart-compile)
(setq smart-compile-alist
(append
'(("\\.rb$" . "ruby %f"))
'(("\\.php$" . "php %f"))
'(("\Gemfile$" . "bundle install"))
smart-compile-alist))&lt;/pre>
&lt;p>もちろん ruby や php を使ってなかったら該当行は必要ありません。&lt;/p>
&lt;h3 id="使い方">使い方&lt;/h3>
&lt;p>これで Gemfile を開いて &lt;code>C-c C-x&lt;/code> するとこんな風に &lt;code>bundle install&lt;/code> が実行されます。&lt;/p>
&lt;pre class="lang:ps decode:true " >-*- mode: compilation; default-directory: "~/dev/foo/" -*-
Compilation started at Sun Dec 15 16:29:42
bundle install
Using rake (10.1.0)
Using i18n (0.6.9) from git://github.com/svenfuchs/i18n.git (at master)
Using minitest (4.7.5)
Using multi_json (1.8.2)
Using atomic (1.1.14)
Using thread_safe (0.1.3)
Using tzinfo (0.3.38)
Using activesupport (4.0.0)
Using builder (3.1.4)
Using erubis (2.7.0)
Using rack (1.5.2)
Using rack-test (0.6.2)
Using actionpack (4.0.0)
Using mime-types (1.25.1)
Using polyglot (0.3.3)
Using treetop (1.4.15)
Using mail (2.5.4)
Using actionmailer (4.0.0)
Using activemodel (4.0.0)
Using activerecord-deprecated_finders (1.0.3)
Using arel (4.0.1)
Using activerecord (4.0.0)
Using sass (3.2.12)
Using bootstrap-sass (3.0.3.0)
Using will_paginate (3.0.5)
Using bootstrap-will_paginate (0.0.10)
Using mini_portile (0.5.2)
Using nokogiri (1.6.0)
Using xpath (2.0.0)
Using capybara (2.2.0)
Using timers (1.1.0)
Using celluloid (0.15.2)
Using ffi (1.9.3)
Using childprocess (0.3.9)
Using coderay (1.1.0)
Using coffee-script-source (1.6.3)
Using execjs (2.0.2)
Using coffee-script (2.2.0)
Using thor (0.18.1)
Using railties (4.0.0)
Using coffee-rails (4.0.1)
Using diff-lcs (1.2.5)
Using factory_girl (4.3.0)
Using factory_girl_rails (4.3.0)
Using faker (1.2.0)
Using formatador (0.2.4)
Using friendly_id (5.0.2)
Using rb-fsevent (0.9.3)
Using rb-inotify (0.9.2)
Using listen (2.4.0)
Using lumberjack (1.0.4)
Using method_source (0.8.2)
Using slop (3.4.7)
Using pry (0.9.12.4)
Using guard (2.2.4)
Using rspec-core (2.14.7)
Using rspec-expectations (2.14.4)
Using rspec-mocks (2.14.4)
Using rspec (2.14.1)
Using guard-rspec (4.2.0)
Using spork (1.0.0rc4)
Using guard-spork (1.5.1)
Using hike (1.2.3)
Using jbuilder (1.5.3)
Using jquery-rails (3.0.4)
Using json (1.8.1)
Using bundler (1.3.5)
Using tilt (1.4.1)
Using sprockets (2.10.1)
Using sprockets-rails (2.0.1)
Using rails (4.0.0)
Using rdoc (3.12.2)
Using rspec-rails (2.14.0)
Using sass-rails (4.0.1)
Using sdoc (0.3.20)
Using spork-rails (4.0.0)
Using sqlite3 (1.3.8)
Using turbolinks (2.0.0)
Using uglifier (2.3.3)
[32mYour bundle is complete![0m
[32mIt was installed into ./vendor/bundle[0m
Compilation finished at Sun Dec 15 16:29:45
&lt;/pre></description></item><item><title>Railsのカラム名の語頭に数字を使ってはいけない</title><link>https://hack.ironsand.net/2013/do-not-use-number-for-column-name-in-rails/</link><pubDate>Sat, 14 Dec 2013 08:08:39 +0000</pubDate><guid>https://hack.ironsand.net/2013/do-not-use-number-for-column-name-in-rails/</guid><description>&lt;p>Railsの学習がてらに2chもどきをつくろうと思って `rails new 2ch` してみるとエラーになる。これはいいんだけど、`rails generate Model 2ch` ってエラーを吐かないのに実際は使えないので修正がめんどくさくなるので気をつけましょう。&lt;/p></description></item><item><title>[deprecated] RailsでI18n.enforce_available_locales とエラーが出る時の対処法</title><link>https://hack.ironsand.net/2013/deprecated-i18n-enforce_available_locales/</link><pubDate>Fri, 13 Dec 2013 15:50:45 +0000</pubDate><guid>https://hack.ironsand.net/2013/deprecated-i18n-enforce_available_locales/</guid><description>&lt;p>Railsでこんなdeprecatedの警告が出てきたので対処方法を調べた。&lt;/p>
&lt;pre class="lang:default decode:true " title="lib/taskの中のファイルを実行した時のエラー" >[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.&lt;/pre>
&lt;h3 id="対策">対策&lt;/h3>
&lt;pre class="lang:ruby decode:true " title="config/application.rb" >config.i18n.enforce_available_locales = true&lt;/pre>
&lt;p>でOK, `locale validation` をきちんとするなら true、どうでも良いならfalseらしい。よくわかんない。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – Rails I18n validation deprecation warning – Stack Overflow][1]&lt;/p></description></item><item><title>link_to で内部にタグがあったりする時はブロックを使いましょう</title><link>https://hack.ironsand.net/2013/link_to-with-block/</link><pubDate>Wed, 11 Dec 2013 06:54:04 +0000</pubDate><guid>https://hack.ironsand.net/2013/link_to-with-block/</guid><description>&lt;p>Zurb-Foundation とか Twitter bootstrap を使ってると、アイコンをリンク内に含めたくなったりしますよね。こんな感じに&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&lt;i class='general foundicon-refresh'>&lt;/i> Reload&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>でもこれをそのまま `link_to` で囲んで&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&amp;lt;%= link_to &amp;ldquo;&lt;i class='general foundicon-refresh'>&lt;/i> Reload&amp;rdquo;,&amp;ldquo;&lt;a href="http://example.com/reload%22">http://example.com/reload&amp;quot;&lt;/a> %&amp;gt;&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>とすると`&lt;i>`タグが HTML エスケープされて悲しい事態になるんです。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>なのでそういう時は `link_to` さんはブロックが取れるのでブロックを使って処理しましょう。ブロックの返り値がリンクのテキストになります。&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&amp;lt;%= link_to &amp;ldquo;&lt;a href="http://example.com/reload%22">http://example.com/reload&amp;quot;&lt;/a>, class: &amp;lsquo;reload&amp;rsquo; do %&amp;gt;&lt;/p>
&lt;p>&lt;i class='general foundicon-refresh'>&lt;/i> Reload&lt;/p>
&lt;p>&amp;lt;% end %&amp;gt;&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>ちなみに `button_to` も同じような関数なのでこういう風にかけます。&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&amp;lt;%= button_to &amp;ldquo;&amp;rdquo;, class: &amp;lsquo;small radius&amp;rsquo; do %&amp;gt;&lt;/p>
&lt;p>&lt;i class='general foundicon-refresh'>&lt;/i> Reload&lt;/p>
&lt;p>&amp;lt;% end %&amp;gt;&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>これ知らない時に必死に HTML エスケープされないために何をすればよいのかを探しまくったのは今では良い思い出でもなんでもありません。時間返せ。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – link_to in helper with block – Stack Overflow][1]&lt;/p></description></item><item><title>既に存在するindexにunique制限をかけるmigrationの書き方。</title><link>https://hack.ironsand.net/2013/how-to-add-unique-constraint-to-already-existing-index-by-migration/</link><pubDate>Mon, 09 Dec 2013 10:22:53 +0000</pubDate><guid>https://hack.ironsand.net/2013/how-to-add-unique-constraint-to-already-existing-index-by-migration/</guid><description>&lt;p>そのまま unique 条件付の index を追加しようとするとエラーになるので&lt;/p>
&lt;p>まず既にある index を削除してから追加しましょう。&lt;/p>
&lt;p>[crayon title=&amp;ldquo;db/migrate/add_unique_constraint.rb&amp;rdquo;]&lt;/p>
&lt;p>def change&lt;/p>
&lt;p>remove_index :editabilities, [:user_id, :list_id]&lt;/p>
&lt;p>add_index :editabilities, [:user_id, :list_id], unique: true&lt;/p>
&lt;p>end&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – How to add &lt;code>unique&lt;/code> constraint to already existing index by migration – Stack Overflow][1]&lt;/p></description></item><item><title>RailsでModelが不規則名詞だった場合のControllerの命名規則</title><link>https://hack.ironsand.net/2013/naming-convention-rails-imodel-irregular-verb/</link><pubDate>Tue, 21 May 2013 07:35:49 +0000</pubDate><guid>https://hack.ironsand.net/2013/naming-convention-rails-imodel-irregular-verb/</guid><description>&lt;p>Rails の Model は常に単数名詞、そして Controller はその名詞の複数形という規則がありますが、Model が”Child&amp;quot;などの不規則変化名詞だった時に Controller 名は&lt;strong>Children なのか、Childs なのかがわからなかった&lt;/strong>ので調べました。&lt;/p>
&lt;h3 id="答え">答え&lt;/h3>
&lt;p>プログラム的には当然簡単に処理できる Childs だと思ったけども実際はちゃんと不規則変化にも対応してるらしく Children が正解です。&lt;/p>
&lt;h3 id="その他の不規則名詞">その他の不規則名詞&lt;/h3>
&lt;p>他の不規則名詞は&lt;/p>
&lt;p>&lt;a href="https://github.com/rails/rails/blob/4-0-0/activesupport/lib/active_support/inflections.rb">https://github.com/rails/rails/blob/4-0-0/activesupport/lib/active_support/inflections.rb&lt;/a>&lt;/p>
&lt;p>に載っているんですが、&lt;/p>
&lt;pre class="wrap:true lang:default decode:true " >inflect.irregular('person', 'people')
inflect.irregular('man', 'men')
inflect.irregular('child', 'children')
inflect.irregular('sex', 'sexes')
inflect.irregular('move', 'moves')
inflect.irregular('cow', 'kine')
inflect.irregular('zombie', 'zombies')
&lt;/pre>
&lt;p>zombie って不規則名詞なの…？&lt;/p>
&lt;p>cow→kine の変化はそもそも英単語がわかりませんでした、、&lt;/p>
&lt;h3 id="zombie-が不規則名詞の理由-20131214-追記">Zombie が不規則名詞の理由 ([2013/12/14] 追記)&lt;/h3>
&lt;p>単数形から複数形への変化は規則通りですが、複数形から単数形への変化 zombies -&amp;gt; zombie が不規則で、規則通り変化させてしまうと zombies -&amp;gt; zomby になってしまうから不規則名詞らしいです。英語は難しいですね。&lt;/p>
&lt;h3 id="情報元">情報元&lt;/h3>
&lt;p>[Model and Controller's naming rule for irregular noun in Rails – Stack Overflow][1]&lt;/p></description></item><item><title>Railsで新規プロジェクトを作ろうとすると`expand_path’: non-absolute homeというエラーが出る</title><link>https://hack.ironsand.net/2013/expand_path-non-absolute-home-error/</link><pubDate>Wed, 01 May 2013 03:12:23 +0000</pubDate><guid>https://hack.ironsand.net/2013/expand_path-non-absolute-home-error/</guid><description>&lt;p>&lt;code>rails new hoge&lt;/code>&lt;/p>
&lt;p>とすると&lt;/p>
&lt;p>&lt;code>`expand_path': non-absolute home&lt;/code>&lt;/p>
&lt;p>というエラーが出てしまって「Windowsだしなあ。」と諦めかけてたけど、このhomeというのは環境変数のHOMEのことらしく、確かにcygwinを使う時のためにHOMEに /cygdrive/ から始まる値を入れてたのを思い出したので環境変数 HOME を削除してみたら動いた。&lt;/p>
&lt;p>ちなみに irb もHOMEに入ってる値によっては動かないらしい。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>`expand_path': non-absolute home&lt;br>
&lt;a href="http://devnet.jetbrains.com/thread/437313">http://devnet.jetbrains.com/thread/437313&lt;/a>&lt;/p></description></item></channel></rss>