LoginSignup
0
1

More than 5 years have passed since last update.

CSVファイルにデーターを出力する。

Last updated at Posted at 2017-04-24

前提

現在RubyでCSVファイルにデーターを出力するのはいろいろな方法ありますが。データーが多い場合、処理スピードが遅いので新しいgemを開発しました。

今回のgemは一気全データを取らずに、リクエストごとに何千件(options[:batch_size]で指定された値)をデータベースから取ります。

使用方法

self.response_body = Exporter.gen_csv(data, headers,
                                                :append => append,
                                                :mappings => MappingHelper.gen_mapping_user())

必須の引数

  1. data

    • アクティブレコードの戻す値です.
    • 例: data = Ad.find(:status => 1).page(20)
  2. headers

    • Rubyの定数だから、そのままでつかえます。

オプション

  1. charset
    • 文字コードです。
    • デフォルトはShift_JIS。
  2. include_column_names
    • カラム名をCSVファイルに出力判断のflagです。
    • デフォルトはtrueです。
  3. structure
    • カラムを順番に出力します。
    • デフォルトは全カラムを出します。
    • 例: ["name", "status", "created_at"]
  4. append
    • 他テブルのカラムもCSVに出します。
    • 例: append = ["ad.name", "ad.price_type", "ad.ext_campaign_id"]
    • 注意: テブル名必要です。
  5. mappings
    • カラムの整数型を文字列に交換します。
    • 例:
     mappings = {
         :users_statuses => {1=>"有効", 2=>"退会済", 3=>"仮登録"}
     }

users:テブル名
status:カラム名(複数はstatuses)

注意

カラム名をCSVファイルに出力したい場合、translation_ja.ymlを下記のように書いてください。

 ja:
   activerecord:
         models:
           account: アカウント
         attributes:
           account: 
              user_name: ユーザ名
              password: パスワード
              password_confirmation: パスワード確認用
              name: 氏名

最後に

みなさん、ご拝読本当にありがとうございました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1