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.:)
source
share