Change barcode to a has_many relation
This commit is contained in:
parent
0885359d71
commit
bfe86ae481
7 changed files with 34 additions and 7 deletions
|
@ -32,7 +32,7 @@ class ProductsController < ApplicationController
|
|||
end
|
||||
|
||||
def from_barcode
|
||||
render json: Product.find_by_barcode(params.require(:barcode))
|
||||
render json: Barcode.find_by_code(params.require(:barcode)).try(:product)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
17
app/models/barcode.rb
Normal file
17
app/models/barcode.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: barcodes
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# product_id :integer
|
||||
# code :string default(""), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
|
||||
class Barcode < ActiveRecord::Base
|
||||
belongs_to :product
|
||||
|
||||
validates :product, presence: true
|
||||
validates :code, uniqueness: true
|
||||
end
|
|
@ -15,13 +15,13 @@
|
|||
# stock :integer default("0"), not null
|
||||
# calories :integer
|
||||
# deleted :boolean default("f")
|
||||
# barcode :string default(""), not null
|
||||
#
|
||||
|
||||
class Product < ActiveRecord::Base
|
||||
include Avatarable
|
||||
|
||||
has_many :order_items
|
||||
has_many :barcodes
|
||||
|
||||
enum category: %w(food beverages other)
|
||||
|
||||
|
@ -29,7 +29,6 @@ class Product < ActiveRecord::Base
|
|||
validates :price_cents, presence: true, numericality: { only_integer: true, greater_than: 0 }
|
||||
validates :stock, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 }
|
||||
validates :barcode, presence: true, uniqueness: true
|
||||
|
||||
scope :for_sale, -> { where deleted: false }
|
||||
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
class AddBarcodeToProducts < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :products, :barcode, :string, null: false, default: ""
|
||||
create_table :barcodes do |t|
|
||||
t.references :product
|
||||
t.string :code, index: true, null: false, default: ""
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
10
db/schema.rb
10
db/schema.rb
|
@ -13,6 +13,15 @@
|
|||
|
||||
ActiveRecord::Schema.define(version: 20150919091214) do
|
||||
|
||||
create_table "barcodes", force: :cascade do |t|
|
||||
t.integer "product_id"
|
||||
t.string "code", default: "", null: false
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
add_index "barcodes", ["code"], name: "index_barcodes_on_code"
|
||||
|
||||
create_table "delayed_jobs", force: :cascade do |t|
|
||||
t.integer "priority", default: 0, null: false
|
||||
t.integer "attempts", default: 0, null: false
|
||||
|
@ -60,7 +69,6 @@ ActiveRecord::Schema.define(version: 20150919091214) do
|
|||
t.integer "stock", default: 0, null: false
|
||||
t.integer "calories"
|
||||
t.boolean "deleted", default: false
|
||||
t.string "barcode", default: "", null: false
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
# stock :integer default("0"), not null
|
||||
# calories :integer
|
||||
# deleted :boolean default("f")
|
||||
# barcode :string default(""), not null
|
||||
#
|
||||
|
||||
require 'faker'
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
# stock :integer default("0"), not null
|
||||
# calories :integer
|
||||
# deleted :boolean default("f")
|
||||
# barcode :string default(""), not null
|
||||
#
|
||||
|
||||
describe Product do
|
||||
|
|
Loading…
Reference in a new issue