前提・実現したいこと
railsでECサイトを作っています。
MySQLのDBにサイトからダウンロードした画像ファイルを直接格納したいのですが、うまくいかないので、ご教授お願い致します。
製品のテーブルに対して、画像のテーブルが1対多の関係になるようにアソシエーションを組んでいます。
画像のサイズは112KBでjpg形式です。
発生している問題・エラーメッセージ
エラーメッセージは特にありませんが、画像ファイルが文字列としてしか格納されていないされていない状態です。
該当のソースコード
ターミナル
INSERT INTO `images` ( `id`, `image`, `created_at`, `updated_at`, `product_id` ) VALUES ( '1', 'publicdomainq-0043630wap.jpg', '2020/01/01', '2020/01/01', '1' );
product.rb
class Product < ApplicationRecord has_many :order_details has_many :orders, through: :order_details has_many :categories, through: :product_categories has_many :product_categories has_many :images end
image.rb
class Image < ApplicationRecord belongs_to :product end
20200604074505_create_images.rb
class CreateImages < ActiveRecord::Migration[5.2] def change create_table :images do |t| t.mediumblob :image, null: false t.references :product, foreign_key: true t.timestamps end end end
20200602075011_create_products.rb
class CreateProducts < ActiveRecord::Migration[5.2] def change create_table :products do |t| t.string :name, null: false t.date :day, null: false t.text :explan, null:false t.integer :price, null: false t.text :voice, null: false end end end
試したこと
imageカラムの型をbinaryに変更してみました。
画像ファイルを入れるディレクトリをpublic直下に変更してみました。
↓こちらのサイトを参考に画像を格納しました。
https://baccholog.com/archives/856
補足情報(FW/ツールのバージョンなど)
mysql Ver 14.14
Rails 5.2.4.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。