LoginSignup
0
0

More than 5 years have passed since last update.

rails/fixtureでstringのカラムにarray.to_jsonの状態で登録する

Posted at

やりたいこと

  • fixtureを利用して、stringのカラムにarray.to_jsonの状態で登録する

参考

解法

  • to_json.inspect が大事

schema

schema.rb
ActiveRecord::Schema.define(version: XXX) do
  create_table 'test_tables' do |t|
    t.string 'string_column' # array.to_json("[\"hoge\",\"foo\"]") の状態で入れるカラム
  end
end

yaml

test/fixture/test_tables.yml
test_data:
 string_column: <%= array.to_json.inspect %>

ダメだった書き方

to_json

test_data:
 string_column: <%= array.to_json %>

to_s

test_data:
 string_column: <%= array.to_s %>

yamlの配列記法

test_data:
 string_column: 
   <% array.each do |item| %>
   - <%= item %>
   <% end %>
0
0
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
0