Attach images in seed.rb file in rails using active storage












1















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'
)









share|improve this question





























    1















    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'
    )









    share|improve this question



























      1












      1








      1








      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'
      )









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 21:40









      hernanvicente

      664615




      664615










      asked Nov 19 '18 at 20:08









      AndersonAnderson

      155




      155
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer
























          • Thank you so much!!

            – Anderson
            Nov 19 '18 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 '18 at 1:22













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.






          share|improve this answer
























          • Thank you so much!!

            – Anderson
            Nov 19 '18 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 '18 at 1:22


















          0














          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.






          share|improve this answer
























          • Thank you so much!!

            – Anderson
            Nov 19 '18 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 '18 at 1:22
















          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 21:03









          hernanvicentehernanvicente

          664615




          664615













          • Thank you so much!!

            – Anderson
            Nov 19 '18 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 '18 at 1:22





















          • Thank you so much!!

            – Anderson
            Nov 19 '18 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 '18 at 1:22



















          Thank you so much!!

          – Anderson
          Nov 19 '18 at 21:15





          Thank you so much!!

          – Anderson
          Nov 19 '18 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 '18 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 '18 at 1:22






















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word