Attach images in seed.rb file in rails using active storage
up vote
1
down vote
favorite
I have a class called vehicles which can have an image attached to it. I have made a default image to display if no other image was uploaded in the vehicles.rb file.
I would like to include images in the seed.rb file so I don't have to manually upload all images. Is this possible?
Help is much appreciated thanks.
Here is my vehicle.rb:
class Vehicle < ApplicationRecord
belongs_to :make
belongs_to :model
accepts_nested_attributes_for :make
accepts_nested_attributes_for :model
has_one_attached :image
after_commit :add_default_image, on: %i[create update]
def add_default_image
unless image.attached?
image.attach(
io: File.open(Rails.root.join('app', 'assets', 'images', 'no_image_available.jpg')),
filename: 'no_image_available.jpg', content_type: 'image/jpg'
)
end
end
end
Here is how i am creating records in my seed file but I would like to include the images as well:
v = Vehicle.create(
vin: '1QJGY54INDG38946',
color: 'grey',
make_id: m.id,
model_id: mo.id,
wholesale_price: '40,000'
)
ruby rails-activestorage
add a comment |
up vote
1
down vote
favorite
I have a class called vehicles which can have an image attached to it. I have made a default image to display if no other image was uploaded in the vehicles.rb file.
I would like to include images in the seed.rb file so I don't have to manually upload all images. Is this possible?
Help is much appreciated thanks.
Here is my vehicle.rb:
class Vehicle < ApplicationRecord
belongs_to :make
belongs_to :model
accepts_nested_attributes_for :make
accepts_nested_attributes_for :model
has_one_attached :image
after_commit :add_default_image, on: %i[create update]
def add_default_image
unless image.attached?
image.attach(
io: File.open(Rails.root.join('app', 'assets', 'images', 'no_image_available.jpg')),
filename: 'no_image_available.jpg', content_type: 'image/jpg'
)
end
end
end
Here is how i am creating records in my seed file but I would like to include the images as well:
v = Vehicle.create(
vin: '1QJGY54INDG38946',
color: 'grey',
make_id: m.id,
model_id: mo.id,
wholesale_price: '40,000'
)
ruby rails-activestorage
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a class called vehicles which can have an image attached to it. I have made a default image to display if no other image was uploaded in the vehicles.rb file.
I would like to include images in the seed.rb file so I don't have to manually upload all images. Is this possible?
Help is much appreciated thanks.
Here is my vehicle.rb:
class Vehicle < ApplicationRecord
belongs_to :make
belongs_to :model
accepts_nested_attributes_for :make
accepts_nested_attributes_for :model
has_one_attached :image
after_commit :add_default_image, on: %i[create update]
def add_default_image
unless image.attached?
image.attach(
io: File.open(Rails.root.join('app', 'assets', 'images', 'no_image_available.jpg')),
filename: 'no_image_available.jpg', content_type: 'image/jpg'
)
end
end
end
Here is how i am creating records in my seed file but I would like to include the images as well:
v = Vehicle.create(
vin: '1QJGY54INDG38946',
color: 'grey',
make_id: m.id,
model_id: mo.id,
wholesale_price: '40,000'
)
ruby rails-activestorage
I have a class called vehicles which can have an image attached to it. I have made a default image to display if no other image was uploaded in the vehicles.rb file.
I would like to include images in the seed.rb file so I don't have to manually upload all images. Is this possible?
Help is much appreciated thanks.
Here is my vehicle.rb:
class Vehicle < ApplicationRecord
belongs_to :make
belongs_to :model
accepts_nested_attributes_for :make
accepts_nested_attributes_for :model
has_one_attached :image
after_commit :add_default_image, on: %i[create update]
def add_default_image
unless image.attached?
image.attach(
io: File.open(Rails.root.join('app', 'assets', 'images', 'no_image_available.jpg')),
filename: 'no_image_available.jpg', content_type: 'image/jpg'
)
end
end
end
Here is how i am creating records in my seed file but I would like to include the images as well:
v = Vehicle.create(
vin: '1QJGY54INDG38946',
color: 'grey',
make_id: m.id,
model_id: mo.id,
wholesale_price: '40,000'
)
ruby rails-activestorage
ruby rails-activestorage
edited Nov 19 at 21:40
hernanvicente
664615
664615
asked Nov 19 at 20:08
Anderson
105
105
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You could use the Ffaker gem to easily generate fake data and finally after creating the vehicle record you can update your record image attribute from the instance variable. Check Attaching File/IO Objects
This would be the code of db/seed.rb file:
if Vehicle.count.zero?
10.times do
v = Vehicle.create(
vin: FFaker::Code.ean,
color: FFaker::Color.name,
maker_id: m.id,
model_id: m.id,
wholesale_price: '40,000'
)
v.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')
end
end
Don't forget to add the ffaker gem to your Gemfile file.
Thank you so much!!
– Anderson
Nov 19 at 21:15
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You could use the Ffaker gem to easily generate fake data and finally after creating the vehicle record you can update your record image attribute from the instance variable. Check Attaching File/IO Objects
This would be the code of db/seed.rb file:
if Vehicle.count.zero?
10.times do
v = Vehicle.create(
vin: FFaker::Code.ean,
color: FFaker::Color.name,
maker_id: m.id,
model_id: m.id,
wholesale_price: '40,000'
)
v.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')
end
end
Don't forget to add the ffaker gem to your Gemfile file.
Thank you so much!!
– Anderson
Nov 19 at 21:15
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
add a comment |
up vote
0
down vote
accepted
You could use the Ffaker gem to easily generate fake data and finally after creating the vehicle record you can update your record image attribute from the instance variable. Check Attaching File/IO Objects
This would be the code of db/seed.rb file:
if Vehicle.count.zero?
10.times do
v = Vehicle.create(
vin: FFaker::Code.ean,
color: FFaker::Color.name,
maker_id: m.id,
model_id: m.id,
wholesale_price: '40,000'
)
v.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')
end
end
Don't forget to add the ffaker gem to your Gemfile file.
Thank you so much!!
– Anderson
Nov 19 at 21:15
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You could use the Ffaker gem to easily generate fake data and finally after creating the vehicle record you can update your record image attribute from the instance variable. Check Attaching File/IO Objects
This would be the code of db/seed.rb file:
if Vehicle.count.zero?
10.times do
v = Vehicle.create(
vin: FFaker::Code.ean,
color: FFaker::Color.name,
maker_id: m.id,
model_id: m.id,
wholesale_price: '40,000'
)
v.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')
end
end
Don't forget to add the ffaker gem to your Gemfile file.
You could use the Ffaker gem to easily generate fake data and finally after creating the vehicle record you can update your record image attribute from the instance variable. Check Attaching File/IO Objects
This would be the code of db/seed.rb file:
if Vehicle.count.zero?
10.times do
v = Vehicle.create(
vin: FFaker::Code.ean,
color: FFaker::Color.name,
maker_id: m.id,
model_id: m.id,
wholesale_price: '40,000'
)
v.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')
end
end
Don't forget to add the ffaker gem to your Gemfile file.
answered Nov 19 at 21:03
hernanvicente
664615
664615
Thank you so much!!
– Anderson
Nov 19 at 21:15
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
add a comment |
Thank you so much!!
– Anderson
Nov 19 at 21:15
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
Thank you so much!!
– Anderson
Nov 19 at 21:15
Thank you so much!!
– Anderson
Nov 19 at 21:15
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
Hi soo i'm trying to enter files for specific records, so I actually have to write the colors and choose an image with the specific color, is there a way to attach different files for each record in the seed.rb?
– Anderson
Nov 22 at 1:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381906%2fattach-images-in-seed-rb-file-in-rails-using-active-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown