Reputation: 15
I got this error when trying to seed database with image file in nested attributes.
rails aborted!
ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: io, filename)
product.rb
class Product < ApplicationRecord
belongs_to :user
has_many :founders, dependent: :destroy
accepts_nested_attributes_for :founders, allow_destroy: true
founder.rb
class Founder < ApplicationRecord
belongs_to :product, optional: true
has_one_attached :profile_picture
end
seed.rb
user = User.create!(email: '[email protected]', password: '1234567890')
10.times do |index|
@product = Product.create(
user: user,
name: "Product #{index+1}",
tagline: Faker::Lorem.sentence,
industry: Faker::Company.industry,
website: Faker::Internet.url,
company_name: Faker::Company.name,
company_website: Faker::Internet.url,
founders_attributes: [
{
name: Faker::Name.name,
email: Faker::Internet.email,
website: Faker::Internet.url,
profile_picture: { io: File.open(Rails.root + "app/assets/images/profile_picture.png"), filename: 'profile_picture.png', content_type: 'image/png' }
},
{
name: Faker::Name.name,
email: Faker::Internet.email,
website: Faker::Internet.url,
profile_picture: { io: File.open(Rails.root + "app/assets/images/profile_picture.png"), filename: 'profile_picture.png', content_type: 'image/png' }
}
]
)
end
I have no idea to solve this error. Give me some advice please.
Upvotes: 0
Views: 164
Reputation: 15288
You can do it without nested attributes. And by the way you didn't close your files:
PNG_PATH = Rails.root.join('app', 'assets', 'images', 'profile_picture.png')
user = User.create!(email: '[email protected]', password: '1234567890')
1.upto(10) do |index|
@product = Product.create(
user: user,
name: "Product #{index}",
tagline: Faker::Lorem.sentence,
industry: Faker::Company.industry,
website: Faker::Internet.url,
company_name: Faker::Company.name,
company_website: Faker::Internet.url
)
2.times do
founder =
@product.founders.create(
name: Faker::Name.name,
email: Faker::Internet.email,
website: Faker::Internet.url
)
File.open(PNG_PATH) do |file|
founder.profile_picture.attach(io: file, filename: File.basename(file.to_path))
end
end
end
Upvotes: 2