Skip to content

Instantly share code, notes, and snippets.

@edenjp
Last active October 26, 2015 14:51
Show Gist options
  • Save edenjp/95a9087e2b182684a96a to your computer and use it in GitHub Desktop.
Save edenjp/95a9087e2b182684a96a to your computer and use it in GitHub Desktop.
1 - rails new concern
rails generate model Product name company:text
rails generate model Category name:text
rails generate model Tag name:text product_id category_id:integer
2 - Arquivo seed
require 'faker'
produtos = (1..100).to_a
categorias = (1..5).to_a
100.times do
Product.create(name: Faker::Name.name, company: Faker::Company.name)
end
5.times do
Category.create(name: Faker::Commerce.department)
end
tagname = ['esporte', 'cama', 'mesa', 'banho', 'eletronico', 'telefonia', 'ferramentas', 'livros' ]
tagname.each {|tag|
Tag.create(name: tagname.sample, product_id: produtos.sample, category_id: categorias.sample )
}
3 e 4 - class Product < ActiveRecord::Base
has_many :tags
def list_tags
tags.map(&:name).join(',')
end
end
class Category < ActiveRecord::Base
has_many :tags
def list_tags
tags.map(&:name).join(',')
end
end
5- module Concerns::Maps
extend ActiveSupport::Concern
def list_tags
tags.map(&:name).join(',')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment