[Ruby][FactoryGirl] Could not find a valid mapping for #<User

Ruby on Rails のプロジェクトにて、rake タスクに FactoryGirl 経由で Devise の user を作成したら、エラー出て死んだ。

■ エラーメッセージ

Could not find a valid mapping for #<User

そして、rake も rails s も rails c も何もできなくなった。

とりあえず、rvm 使っていたので、gemset を作り直すことで rake や rails s などは実行できるようになった。

しかし、User登録については、まだ解決できてない。ダレカタスケテ

【参考】

Reloading Factory Girl Factories in the Rails 3 Console | Transcending Frontiers はてなブックマーク - Reloading Factory Girl Factories in the Rails 3 Console | Transcending Frontiers

devise – “Could not find a valid mapping for #” only on second and successive tests – Stack Overflow はてなブックマーク - devise - ” only on second and successive tests – Stack Overflow” border=”0″ />

[Ruby][RVM] mysql2.bundle: [BUG] Segmentation fault

Ruby on Rails のプロジェクトにて rvm の gemset を再作成(delete → create)後に bundle install して gem をインストールしてから rails s したら abort した問題を解決した話をご紹介します。

続きを読む

[Rails][FactoryGirl] エラー:Factory already registered: xxx

Ruby on Railsにて、

Factory already registered: xxx

lib/tasks/

require 'factory_girl'
Dir[Rails.root.join('spec/factories/*.rb')].each {|f| require f }

【参考】ruby on rails – Factory already registered: user (FactoryGirl::DuplicateDefinitionError) – Stack Overflow はてなブックマーク - ruby on rails - Factory already registered: user (FactoryGirl::DuplicateDefinitionError) - Stack Overflow

[Ruby] サンプルデータ作成が便利なGem「faker」

Ruby でテストデータやサンプルデータを作成するときに「faker」というGemを使うと捗りそう。

faker はてなブックマーク - faker

Class: Faker::Internet — Documentation for stympy/faker (master) はてなブックマーク - Class: Faker::Internet — Documentation for stympy/faker (master)

blog.shu-cream.net: Ruby(Rails)で適当なサンプルデータを作るにはfakerかForgery はてなブックマーク - blog.shu-cream.net: Ruby(Rails)で適当なサンプルデータを作るにはfakerかForgery

[Ruby on Rails] 多対多の関連テーブルの作成方法

Ruby on Rails にて、多対多の関連テーブルの作成方法をメモ。

「Rails3レシピブック」の[076]「関連をまたいだ関連を設定する」を参照する。

Railsの多対多で関連テーブルの操作色々 – Tech<3 by 榊祐介 - s21g はてなブックマーク - Railsの多対多で関連テーブルの操作色々 - Tech<3 by 榊祐介 - s21g

[Ruby] Rails(ActiveRecord)の多対多関連 – @yuumi3のお仕事日記 はてなブックマーク - [Ruby] Rails(ActiveRecord)の多対多関連 - @yuumi3のお仕事日記

Railsで多対多のモデルを簡単に扱えるhas_many :through – このブログは証明できない。 はてなブックマーク - Railsで多対多のモデルを簡単に扱えるhas_many :through - このブログは証明できない。

Ruby on Rails : テーブル間リレーションシップ – WebOS Goodies はてなブックマーク - Ruby on Rails : テーブル間リレーションシップ - WebOS Goodies

[Ruby on Rails] seed データの追加や管理をカンタンにしてくれる seed-fu の使い方

Ruby on Rails で seed データの追加や管理をカンタンにしてくれる seed-fu gem が便利なのでご紹介します。

使い方は db/fixtures 以下に users.rb とかいうファイル名で、簡単には下記のような感じで書きます。

db/fixtures/users.rb

User.seed do |s|
  s.id    = 1
  s.login = "jon"
  s.email = "[email protected]"
  s.name  = "Jon"
end
 
User.seed do |s|
  s.id    = 2
  s.login = "emily"
  s.email = "[email protected]"
  s.name  = "Emily"
end

全ての seed ファイルからインポートするには、単に

rake db:seed_fu

を実行すればよいです。

ファイルやモデル別にロードするには、下記のような感じで rake タスクを実行する。

rake db:seed_fu FIXTURE_PATH=path/to/fixtures
rake db:seed_fu FILTER=users,articles

当然だが Ruby コードが書けるので、こんな感じに CSV から DB へインポートみたいなことも書ける。

db/fixtures/users.rb

require 'csv'
 
csv = CSV.read('db/fixtures/users_master.csv')
 
csv.each_with_index do |user, i|
  # skip a label row
  next if i === 0
 
  name = user[0]
  age  = user[1].to_i
 
  User.seed do |s|
    s.id   = i
    s.name = name
    s.age  = age
  end
end

地味に便利ですね。

参考情報

[Ruby] ページング機能を追加する「will_paginate」

Rubyでページング機能を追加するGem「will_paginate」の使い方をメモ。

mislav/will_paginate はてなブックマーク - mislav/will_paginate

Home · mislav/will_paginate Wiki はてなブックマーク - Home · mislav/will_paginate Wiki

Translating will_paginate output (i18n) · mislav/will_paginate Wiki はてなブックマーク - Translating will_paginate output (i18n) · mislav/will_paginate Wiki

will_paginate | RubyGems.org | your community gem host はてなブックマーク - will_paginate | RubyGems.org | your community gem host

Rails 3 + will_paginate 3.0.pre2でAjax対応なリンクを生成する | TechRacho はてなブックマーク - Rails 3 + will_paginate 3.0.pre2でAjax対応なリンクを生成する | TechRacho

Ruby on Rails 3でwill_paginateを使ってみた。 – cys b はてなブックマーク - Ruby on Rails 3でwill_paginateを使ってみた。 - cys b

■ オプション一覧

# ==== Options
# * <tt>:class</tt> -- CSS class name for the generated DIV (default: "pagination")
# * <tt>:previous_label</tt> -- default: "« Previous"
# * <tt>:next_label</tt> -- default: "Next »"
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
# * <tt>:inner_window</tt> -- how many links are shown around the current page (default: 4)
# * <tt>:outer_window</tt> -- how many links are around the first and the last page (default: 1)
# * <tt>:link_separator</tt> -- string separator for page HTML elements (default: single space)
# * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
# * <tt>:params</tt> -- additional parameters when generating pagination links
#   (eg. <tt>:controller => "foo", :action => nil</tt>)
# * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default in Rails:
#   <tt>WillPaginate::ActionView::LinkRenderer</tt>)
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
# * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
#   false only when you are rendering your own pagination markup (default: true)

will_paginate/lib/will_paginate/view_helpers.rb at master · mislav/will_paginate はてなブックマーク - will_paginate/lib/will_paginate/view_helpers.rb at master · mislav/will_paginate

will_paginateのオプション設定でpaginationの外観を変更する方法 – memo.yomukaku.net はてなブックマーク - will_paginateのオプション設定でpaginationの外観を変更する方法 - memo.yomukaku.net

will_paginateヘルパーのオプション覚書 – screw it! はてなブックマーク - will_paginateヘルパーのオプション覚書 - screw it!

国際化対応(i18n対応)

i18n対応だけど、ここで初期値をセットしておくと楽。

■ config/locales/will_paginate.ja.yml

ja:
  will_paginate:
    previous_label: "&lt;"
    next_label: "&gt;"
    page_gap: "..."

ページング出力のHTMLタグカスタマイズ

Roll your own pagination links with will_paginate – UK Specialists in Ruby on Rails, Exalead, AWS, Consultancy はてなブックマーク - Roll your own pagination links with will_paginate – UK Specialists in Ruby on Rails, Exalead, AWS, Consultancy

[Ruby on Rails] find で ActiveRecord::RecordNotFound

Rails にて find メソッドで id のみを引数で渡して、結果レコードが0件の場合、ActiveRecord::RecordNotFound の例外が発生してしまいます。

例外処理をするもよし。

rescue ActiveRecord::RecordNotFound

find_by_id を使えば、結果が0件の場合 nil が返ってくるので、find_by_id を使うもよし。

【参考】

findよりfind_by_idでActiveRecord::RecordNotFoundを回避する – パンプキンスパイスラテ はてなブックマーク - findよりfind_by_idでActiveRecord::RecordNotFoundを回避する - パンプキンスパイスラテ

[Objective-C][iOS] TabBar(タブバー)の実装方法

iPhone/iPadアプリ開発で、TabBar(タブバー)の実装方法を調べたのでメモ。

この辺を読む。

xcode4.2でStorybordを使わずにtabBarControllerにnavigationControllerを追加する方法 – tmplog はてなブックマーク - xcode4.2でStorybordを使わずにtabBarControllerにnavigationControllerを追加する方法 - tmplog

Bugle Diary: [Objective-C][iPhone sdk]tabbarの実装 はてなブックマーク - Bugle Diary: [Objective-C][iPhone sdk]tabbarの実装

頭と尻尾はくれてやる! タブ+ナビゲーションでタイトルとアイコンの設定 はてなブックマーク - 頭と尻尾はくれてやる! タブ+ナビゲーションでタイトルとアイコンの設定

UITabBarController – iPhoneアプリ開発の虎の巻 はてなブックマーク - UITabBarController - iPhoneアプリ開発の虎の巻

UINavigationController – iPhoneアプリ開発の虎の巻 はてなブックマーク - UINavigationController - iPhoneアプリ開発の虎の巻

以上です。

[Objective-C][iOS] unable to dequeue a cell with identifier Cell – must register a nib or a class for the identifier or connect a prototype cell in a storyboar

iOSアプリ開発にて、UITableView でエラー発生。

■ エラーメッセージ

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘unable to dequeue a cell with identifier Cell – must register a nib or a class for the identifier or connect a prototype cell in a storyboard’

■ 解決方法

indexPathに対応するCellのUIが取得できないので、indexPath指定せずに、後で値を取得するように修正すればOKです。

//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; //これだと実行時エラー
 
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

【参考】:UITableView dequeueReusableCellWithIdentifier:forIndexPath: でのアサーション #iOS #Objective-C – Qiita はてなブックマーク - UITableView dequeueReusableCellWithIdentifier:forIndexPath: でのアサーション #iOS #Objective-C - Qiita