Skip to content

Instantly share code, notes, and snippets.

@RJNY
Created February 10, 2017 19:16
Show Gist options
  • Save RJNY/b0d60341bfa2d6787066448477f046e6 to your computer and use it in GitHub Desktop.
Save RJNY/b0d60341bfa2d6787066448477f046e6 to your computer and use it in GitHub Desktop.
code-sample-2
##################################################
##### app/models/piece.rb #####
##################################################
class Piece < ApplicationRecord
belongs_to :category
has_many :tags, as: :taggable
has_many :images, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :images
end
##################################################
###### app/models/image.rb #####
##################################################
class Image < ApplicationRecord
belongs_to :imageable, polymorphic: true
mount_uploader :img, ImageUploader
before_create :default_name
def default_name
title ||= File.basename(img.filename, '.*').titleize if img.present? && title.blank?
end
end
##################################################
###### db/migrate/8675309_create_images.rb #####
##################################################
class CreateImages < ActiveRecord::Migration[5.0]
def change
create_table :images do |t|
t.string :img
t.string :imageable_type
t.integer :imageable_id
t.timestamps
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment