Skip to content

Instantly share code, notes, and snippets.

@aristotelesbr
Created January 26, 2015 18:43
Show Gist options
  • Save aristotelesbr/aaf1a272be442b8bcd6e to your computer and use it in GitHub Desktop.
Save aristotelesbr/aaf1a272be442b8bcd6e to your computer and use it in GitHub Desktop.
Criar uma nova venda somente se o valor em Produtos for maior que 0.
def create
@sale = Sale.new(sale_params)
if @sale.amount <= @product.amount
redirect_to root_path, :notice => "Quantidade indisponível"
else
@sale.save
respond_with(@sale)
end
end
@dodops
Copy link

dodops commented Jan 26, 2015

Não sei como está sua lógica de Models, eu faria algo parecido com isso, um before_save que checasse o sale.amount.

class Sale < ActiveRecord::Base
  before_save :nomedavali

  protected 

  def nomedavali
    self.amount <= product.amount
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment