Ruby on Rails: Check the Number of Products Owned by the Store

I was busy with a test project to better understand Rails.

In my case, I have three models: Store, User, and Product.

The store can be of three types: basic, medium, large. Basic can have 10 products maximum, average 50, large 100.

I am trying to check this type of data, type Shop, and check how many products it owns when creating a new product.

So far I have come up with this code (in shop.rb), but it does not work:

  def lol
      account = Shop.find_by_sql "SELECT account FROM shops WHERE user_id = 4 LIMIT 1"
    products = Product.count_by_sql "SELECT COUNT(*) FROM products WHERE shop_id = 13"
    if account = 1 && products >= 10
        raise "message"
    elsif   account = 2 && products >= 50
        raise "message"
    else account = 3 && products >= 100
        raise "message"
    end
end

I don’t even know if the logic of my decision is correct or what. Maybe I should check usage

has_many

and its size method? I dont know.:)

+3
source share
6 answers

, account = 1 account == 1. account = 2 account = 3.

Rails Guides, , Rails.

, - :

class Shop < ActiveRecord::Base
  has_many :products
  validates :products_within_limit

  # Instead of the 'account' column, you could make a 'max_size' column.
  # Then you can simply do:
  def products_within_limit
    if products.size > max_size
      errors.add_to_base("Shop cannot own more products than its limit")
    end
  end

  def is_basic?
    products.size >= 10 && products.size < 50 
  end

  def is_medium?
    products.size >= 50 && products.size < 100
  end

  def is_big?
    products.size >= 100
  end

  def shop_size
    if self.is_basic?
      'basic'
    elsif self.is_medium?
      'medium'
    elsif self.is_big?
      'big'
    end
  end
end

:

# Get shop with id = 1
shop = Shop.find(1)

# Suppose shop '1' has 18 products:
shop.is_big? # output false
shop.is_medium? # output false
shop.is_basic? # output true
shop.shop_size # output 'basic'
+6

Rails-y. Ruby (Shop). YMMV.

EDIT: , "account" OP ActiveRecord " " , ' type ", , , . OP, , , STI - .

EDIT: , , / . . .

class Product < ActiveRecord::Base
  belongs_to :shop
end

class Shop < ActiveRecord::Base
  has_many :products  
  belongs_to :user
  validates :products_within_limit

  def products_within_limit
    if products.count > limit
      errors.add_to_base("Shop cannot own more products than its limit")
    end
  end

  def limit
    raise "limit must be overridden by a subclass of Shop."
  end
end

class BasicShop < Shop
  def limit
    10
  end
end

class MediumShop < Shop
  def limit
    50
  end
end

class LargeShop < Shop
  def limit
    100
  end
end

shop = BasicShop.create
10.times {Product.create(:shop => shop)}
shop.reload
shop.valid? # is true
shop.products << Product.new
shop.valid? # is false
+3

:

class Shop < ActiveRecord::Base
  has_many :products

  validate :check_nr_of_products

  def check_nr_of_products
    nr_of_products = products.size
    errors[:base] << "Basic shops can have max 10 products" if account == 1 && nr_of_products > 10
    errors[:base] << "Medium shops can have max 50 products" if account == 2 && nr_of_products > 50
    errors[:base] << "Big shops can have max 100 products" if account == 3 && nr_of_products > 100
  end        

. " ", , . , , , size, .

. STI, @Dave_Sims, -.

+3

.

+1

, , .:)

, , . , , , , .:(

:

class Shop < ActiveRecord::Base

belongs_to :user
has_many :products, :dependent => :destroy
validate :is_account                                                

def is_account
    if account == 1 && products.size < 11
    elsif account == 2 && products.size < 51
    else account == 3 && products.size < 101
    end
end

products_controller.rb :

def new
if current_user.shop.nil?
    flash[:notice] = I18n.t 'shops.create.must' #this should check is the user owns a shop, otherwise can't add a product. It seems to work, so far
    redirect_to :action => :index
elsif current_user.shop.valid?
    flash[:notice] = I18n.t 'shops.upgrade.must'
    redirect_to :action => :index
  else
    @product = Product.new
  end
end

1 9 , , " ", /products shops.upgrade.must.

, ,

account 

shop.rb . int (11), , , ...

0

, . , :

#in shop.rb
validate :is_account                                                

def is_account
    if account == 1
        limit = 10
    elsif account == 2
        limit = 50
    else account == 3
        limit = 100
    end
    errors.add(:base, "Reached maximum number of items for shop") if account == account && products.size >= limit
end

#in products_controller.rb

def new
if current_user.shop.nil?
    flash[:alert] = I18n.t 'shops.create.must'
    redirect_to :action => :index
elsif current_user.shop.invalid?
    flash[:alert] = I18n.t 'shops.upgrade.must'
    redirect_to :action => :index
else
    @product = Product.new
  end
end

, . , .

!:)

0

Source: https://habr.com/ru/post/1769125/


All Articles