Save 36% for Black Friday! Learn more

oscar martinez

Joined

40 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in How do I create a virtual balance model in Rails?

I was trying as well with this diferent approach with code similar to the code Rohan use. But its not working.
Any sugestion?

class OrdersController < ApplicationController

def create
@order = current_user.orders.create(order_params)
redirect_to user_orders_path
end
end

def balance
if @order.save
@user.balance_update!(balance: @user.balance + @order.price)
end
end

private
def order_params
params.require(:order).permit(:price, :user_id)
end
end

Posted in How do I create a virtual balance model in Rails?

This related I have a question:

I am trying the amount of @order pass and update the @balance. In my models both are integers.
I am trying this from Orders_controller but doesnt work.
Do you have any sugestion?
Do you need to see more code?
Any comment is welcome!!

class OrdersController < ApplicationController

def create
@order = current_user.orders.create(order_params)
@user.balance = balance: (@user.balance += @order.price)
redirect_to user_orders_path
end
end

private
def order_params
params.require(:order).permit(:price, :user_id)
end
end

Posted in How do I create a virtual balance model in Rails?

And dont forget that balance is an attribute in User model

Posted in How do I create a virtual balance model in Rails?

Hi every one:

I am having similar challenges regarding the creation of a payment system with a type of credits (simple virtual money).
Regarding the balance I am using this method in the User model and is working fine. Just in the case Rohan has some issues with Casey sugestion.

before_validation :load_defaults

def load_defaults
if self.new_record?
self.balance = 1000
end
end

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.