前提・実現したいこと
railsでseedファイルを作成しています。
origin_itemを作成中に以下のエラーメッセージが出ました。
発生している問題・エラーメッセージ
NoMethodError: undefined method `each' for 5:Integer
該当のソースコード
5.times do |n| @origin_item = OriginItem.create!( item_group_id: @item_group.id, title: "サンプルアイテム#{n+1}", overview: "アイテム#{n+1}", status: rand(0..1) ) end
ちなみに、item_groupは以下になります。
5.times do |n| @item_group = ItemGroup.create!( category_id: @category.id, icon: open("db/images/sample.png"), start_date: "2020/5/1", end_date: "2020/5/30", title: "チュートリアル#{n+1}", overview: "アイテムの購入について", status: rand(0..1) ) end
試したこと
eachのエラーから5個も繰り返せる配列がないとのエラーだと思い、5.timesの記載を消して試してみましたが、解決できませんでした。何か対処法あれば教えて頂きたいです。
seed
1require 'csv' 2require 'securerandom' 3##master系CSVの読み込み 4#category 5CSV.foreach("db/csv/masters/init_categories.csv") do |row| 6 @category = Masters::Category.create!(name: row[0]) 7end 8 9#tag 10CSV.foreach("db/csv/masters/init_tags.csv") do |row| 11 @tag = Masters::Tag.create!(name: row[0]) 12end 13 14#year 15CSV.foreach("db/csv/masters/init_years.csv") do |row| 16@year = Masters::Year.create!(name: row[0]) 17end 18 19## Master系以外 20#admin user active 21adminuser = User.create!( 22 email: "test@admin.com", 23 password: "password", 24 user_type: 1, 25 status: 1 ) 26 27#user 2810.times do |n| 29 @user = User.create!( 30 email: "user#{n+1}@example.com", 31 password: "password", 32 user_type: 0, 33 status: 0 ) 34end 35 36#line_profile 37User.all.each_with_index do |user,idx| 38 line_profile = LineProfile.create!( 39 user_id: user.id, 40 line_id: "@testline#{idx+1}" ) 41end 42 43#profile 4410.times do |n| 45 @profile = Profile.create!( 46 user_id: Random.rand(10) + 1, 47 name: "山田太郎", 48 phonetic: "やまだたろう", 49 phone_number: "09000000000", 50 postal_code: "123-1234", 51 address: "東京都中央区XXX-XXXXXXXX" ) 52end 53 54#item_topic 5510.times do |n| 56 @item_topic = ItemTopic.create!( 57 thumbnail: open("db/images/sample.png"), 58 title: "商品トピック#{n+1}", 59 overview: "概要#{n+1}", 60 link: "http://origin_item_topic_sample.com", 61 status: rand(0..1) ) 62end 63 64#favorites 6510.times do 66 favorite = Favorite.create!( 67 user_id: Random.rand(10) + 1, 68 job_topic_id: Random.rand(10) + 1 ) 69end 70 71#item_topic_tag 7210.times do 73 ItemTopicTag.create!(tag_id: @tag.id, job_topic_id: @item_topic.id) 74end 75#item_topic_category 76item_topic_category = JobTopicCategory.create!(job_topic_id: @item_topic.id, category_id: @category.id) 77 78#item_event 7910.times do |n| 80 @item_event = ItemEvent.create!( 81 title: "商品説明会#{n+1}", 82 link: "http://example#{n+1}.com", 83 image: open("db/images/sample.png"), 84 color: "blue", 85 start_date: "2020/5/1", 86 end_date: "2020/5/30", 87 status: rand(0..1) ) 88end 89 90#item_site 9110.times do |n| 92 @item_site = ItemSite.create!( 93 thumbnail: open("db/images/sample.png"), 94 logo: open("db/images/sample.png"), 95 image: open("db/images/sample.png"), 96 description: "説明#{n+1}", 97 title: "商品サービス#{n+1}", 98 overview: "概要#{n+1}", 99 link: "http://origin_item_site_sample.com" ) 100end 101 102#item_site_tqg 103item_site_tag = ItemSiteTag.create!(item_site_id: @item_site.id, tag_id: @tag.id) 104 105#item_site_review 10610.times do |n| 107 @item_site_review = ItemSiteReview.create!( 108 item_site_id: @item_site.id, 109 thumbnail: open("db/images/sample.png"), 110 title: "最高でした", 111 link: "http://examplereview#{n}.com" ) 112end 113 114#item_site_category 115item_site_category = ItemSiteCategory.create!(item_site_id: @item_site.id, category_id: @category.id) 116 117#item_group 1185.times do |n| 119 @item_group = ItemGroup.create!( 120 category_id: @category.id, 121 icon: open("db/images/sample.png"), 122 start_date: "2020/5/1", 123 end_date: "2020/5/30", 124 title: "チュートリアル#{n+1}", 125 overview: "アイテム購入について", 126 status: rand(0..1) ) 127end 128#origin_item 1295.times do |n| 130 @origin_item = OriginItem.create!( 131 item_group_id: @item_group.id, 132 title: "サンプルアイテム#{n+1}", 133 overview: "アイテム#{n+1}", 134 status: rand(0..1) ) 135end
エラー文全文です。
/usr/local/bundle/gems/acts_as_paranoid-0.6.3/lib/acts_as_paranoid/associations.rb:19: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/associations.rb:1657: warning: The called method `belongs_to_without _deleted' is defined here /usr/local/bundle/gems/sorcery-0.15.0/lib/sorcery/adapters/active_record_adapter.rb:38: warning: Using the last argument as keywor d parameters is deprecated; maybe ** should be added to the call /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/callbacks.rb:144: warning: The called method is defined here rails aborted! NoMethodError: undefined method `each' for 5:Integer /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/associations/collection_association.rb:245:in `replace' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/associations/collection_association.rb:41:in `writer' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/associations/builder/association.rb:108:in `target_years=' /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/attribute_assignment.rb:50:in `public_send' /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/attribute_assignment.rb:50:in `_assign_attribute' /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/attribute_assignment.rb:43:in `block in _assign_attributes' /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/attribute_assignment.rb:42:in `each' /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/attribute_assignment.rb:42:in `_assign_attributes' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/attribute_assignment.rb:21:in `_assign_attributes' /usr/local/bundle/gems/activemodel-6.0.3.2/lib/active_model/attribute_assignment.rb:35:in `assign_attributes' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/core.rb:325:in `initialize' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/inheritance.rb:70:in `new' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/inheritance.rb:70:in `new' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/persistence.rb:54:in `create!' /my-api/db/seeds.rb:185:in `block in <main>' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/relation/delegation.rb:87:in `each' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/relation/delegation.rb:87:in `each' /my-api/db/seeds.rb:184:in `<main>' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/engine.rb:557:in `block in load_seed' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/engine.rb:675:in `with_inline_jobs' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/engine.rb:557:in `load_seed' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/tasks/database_tasks.rb:440:in `load_seed' /usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/railties/databases.rake:331:in `block (2 levels) in <main>' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsna p_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsna p_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require' /usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:291:in `load_dependency' /usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `require' /my-api/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' /my-api/bin/spring:15:in `require' /my-api/bin/spring:15:in `<top (required)>' bin/rails:3:in `load'
エラーはどの行に出ていますか?(書かれているコードにeachがなさそうです)
エラーは seeds.rbの@origin_item = OriginItem.create!の行でています。
他のコントローラー等の設定をみるべきなのでしょうか?
気持ちが悪いのは インスタンス変数が使われていること。。。
ということはエラーのでているloopの前にもcodeがあるとおもいます。
頭からそこまで載せてください。そこに何かあるのかもしれません
@origin_itemはどこかの関連でidを持って来るときのために一応設定しているだけで現在は使用しなくても良いものになっています。
ですので、@origin_itemはここでしか現在定義していないです。
@item_groupのことを言ってます
失礼しました。下記にコード載せます。require 'csv'
require 'securerandom'
##master系CSVの読み込み
#category
CSV.foreach("db/csv/masters/init_categories.csv") do |row|
@category = Masters::Category.create!(name: row[0])
end
#tag
CSV.foreach("db/csv/masters/init_tags.csv") do |row|
@tag = Masters::Tag.create!(name: row[0])
end
#year
CSV.foreach("db/csv/masters/init_years.csv") do |row|
@year = Masters::Year.create!(name: row[0])
end
## Master系以外
#admin user active
adminuser = User.create!(
email: "test@admin.com",
password: "password",
user_type: 1,
status: 1 )
#user
10.times do |n|
@user = User.create!(
email: "user#{n+1}@example.com",
password: "password",
user_type: 0,
status: 0 )
end
#line_profile
User.all.each_with_index do |user,idx|
line_profile = LineProfile.create!(
user_id: user.id,
line_id: "@testline#{idx+1}" )
end
#profile
10.times do |n|
@profile = Profile.create!(
user_id: Random.rand(10) + 1,
name: "山田太郎",
phonetic: "やまだたろう",
phone_number: "09000000000",
postal_code: "123-1234",
address: "東京都中央区XXX-XXXXXXXX" )
end
#item_topic
10.times do |n|
@item_topic = ItemTopic.create!(
thumbnail: open("db/images/sample.png"),
title: "商品トピック#{n+1}",
overview: "概要#{n+1}",
link: "http://origin_item_topic_sample.com",
status: rand(0..1) )
end
#favorites
10.times do
favorite = Favorite.create!(
user_id: Random.rand(10) + 1,
job_topic_id: Random.rand(10) + 1 )
end
#item_topic_tag
10.times do
ItemTopicTag.create!(tag_id: @tag.id, job_topic_id: @item_topic.id)
end
#item_topic_category
item_topic_category = JobTopicCategory.create!(job_topic_id: @item_topic.id, category_id: @category.id)
#item_event
10.times do |n|
@item_event = ItemEvent.create!(
title: "商品説明会#{n+1}",
link: "http://example#{n+1}.com",
image: open("db/images/sample.png"),
color: "blue",
start_date: "2020/5/1",
end_date: "2020/5/30",
status: rand(0..1) )
end
#item_site
10.times do |n|
@item_site = ItemSite.create!(
thumbnail: open("db/images/sample.png"),
logo: open("db/images/sample.png"),
image: open("db/images/sample.png"),
description: "説明#{n+1}",
title: "商品サービス#{n+1}",
overview: "概要#{n+1}",
link: "http://origin_item_site_sample.com" )
end
#item_site_tqg
item_site_tag = ItemSiteTag.create!(item_site_id: @item_site.id, tag_id: @tag.id)
#item_site_review
10.times do |n|
@item_site_review = ItemSiteReview.create!(
item_site_id: @item_site.id,
thumbnail: open("db/images/sample.png"),
title: "最高でした",
link: "http://examplereview#{n}.com" )
end
#item_site_category
item_site_category = ItemSiteCategory.create!(item_site_id: @item_site.id, category_id: @category.id)
@origin_item = OriginItem.create! がどこにもみえませんが。。
この直後にあるということ?
@item_groupもないね。。。
それと、プログラムはここに載せられると読みにくいので、質問欄に<code>で書いて欲しい
失礼しました。見にくくなってしまい申し訳ありません。
次回から<code>に記載します。
@origin_itemはこの直後に記載しています。
@item_groupは?
すみません。再度全部<code>で載せます。
```seed
require 'csv'
require 'securerandom'
##master系CSVの読み込み
#category
CSV.foreach("db/csv/masters/init_categories.csv") do |row|
@category = Masters::Category.create!(name: row[0])
end
#tag
CSV.foreach("db/csv/masters/init_tags.csv") do |row|
@tag = Masters::Tag.create!(name: row[0])
end
#year
CSV.foreach("db/csv/masters/init_years.csv") do |row|
@year = Masters::Year.create!(name: row[0])
end
## Master系以外
#admin user active
adminuser = User.create!(
email: "test@admin.com",
password: "password",
user_type: 1,
status: 1 )
#user
10.times do |n|
@user = User.create!(
email: "user#{n+1}@example.com",
password: "password",
user_type: 0,
status: 0 )
end
#line_profile
User.all.each_with_index do |user,idx|
line_profile = LineProfile.create!(
user_id: user.id,
line_id: "@testline#{idx+1}" )
end
#profile
10.times do |n|
@profile = Profile.create!(
user_id: Random.rand(10) + 1,
name: "山田太郎",
phonetic: "やまだたろう",
phone_number: "09000000000",
postal_code: "123-1234",
address: "東京都中央区XXX-XXXXXXXX" )
end
#item_topic
10.times do |n|
@item_topic = ItemTopic.create!(
thumbnail: open("db/images/sample.png"),
title: "商品トピック#{n+1}",
overview: "概要#{n+1}",
link: "http://origin_item_topic_sample.com",
status: rand(0..1) )
end
#favorites
10.times do
favorite = Favorite.create!(
user_id: Random.rand(10) + 1,
job_topic_id: Random.rand(10) + 1 )
end
#item_topic_tag
10.times do
ItemTopicTag.create!(tag_id: @tag.id, job_topic_id: @item_topic.id)
end
#item_topic_category
item_topic_category = JobTopicCategory.create!(job_topic_id: @item_topic.id, category_id: @category.id)
#item_event
10.times do |n|
@item_event = ItemEvent.create!(
title: "商品説明会#{n+1}",
link: "http://example#{n+1}.com",
image: open("db/images/sample.png"),
color: "blue",
start_date: "2020/5/1",
end_date: "2020/5/30",
status: rand(0..1) )
end
#item_site
10.times do |n|
@item_site = ItemSite.create!(
thumbnail: open("db/images/sample.png"),
logo: open("db/images/sample.png"),
image: open("db/images/sample.png"),
description: "説明#{n+1}",
title: "商品サービス#{n+1}",
overview: "概要#{n+1}",
link: "http://origin_item_site_sample.com" )
end
#item_site_tqg
item_site_tag = ItemSiteTag.create!(item_site_id: @item_site.id, tag_id: @tag.id)
#item_site_review
10.times do |n|
@item_site_review = ItemSiteReview.create!(
item_site_id: @item_site.id,
thumbnail: open("db/images/sample.png"),
title: "最高でした",
link: "http://examplereview#{n}.com" )
end
#item_site_category
item_site_category = ItemSiteCategory.create!(item_site_id: @item_site.id, category_id: @category.id)
#item_group
5.times do |n|
@item_group = ItemGroup.create!(
category_id: @category.id,
icon: open("db/images/sample.png"),
start_date: "2020/5/1",
end_date: "2020/5/30",
title: "チュートリアル#{n+1}",
overview: "アイテム購入について",
status: rand(0..1) )
end
#origin_item
5.times do |n|
@origin_item = OriginItem.create!(
item_group_id: @item_group.id,
title: "サンプルアイテム#{n+1}",
overview: "アイテム#{n+1}",
status: rand(0..1) )
end
```
次回から<code>に記載します。 はどうなったの?
書き直したつもりだったのすが、変わっていなかったので少々お待ちください。
すみません。
質問投稿欄更新しました。
ご確認お願い致します。
読みやすくなりました。
が、、、
可能性があるかなぁと思っていた「@item_groupにおかしな値が入っている」という線がなくなってしまった。
maisumakunさんの言われるように eachがないですね。
となると RAILSかgemかのどこかで起きているので、エラーメッセージを全部載せていただかないとわからないです。
/my-api/db/seeds.rb:185:in `block in <main>'
とあります。でも載っている seeds.rbは135行しか無いです。
もっと先に問題があるようです
すみません。いくつかitemと関係のないseedデータを追加したので、行数が増えてしまっています。エラーの箇所は@origin_itemのところであっています。
回答1件
あなたの回答
tips
プレビュー