jeudi 9 avril 2015

Rails model if statement

I'm new to rails and app development so forgive any mistakes.


I've got a model called Product which has the following (schema):



t.string "name"
t.integer "cost"
t.boolean "in_stock"
t.datetime "sold_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "quantity"


I want the in_stock to return true if the quantity is greater than 1, otherwise false. I wrote the following code in the product.rb file but it doesn't seem do anything when I enter the product quantities through the console. I'm not sure if I have to link the database columns (in_stock and quantity) to the if statement or not. Or if even this is the right way to go about things. I'd be grateful for any suggestions. Thanks!



class Product < ActiveRecord::Base
belongs_to :company

def in_stock
if quantity >= 1
in_stock true
else
in_stock false
end

Aucun commentaire:

Enregistrer un commentaire