NoMethodError - Calculation in Rails Model - ruby-on-rails

I have an item model. In that model I need to be able to calculate the profit of a product. However, if I just bought that item it obviously has not been purchased yet and the values for sold_for, fees, and shipping will be empty. If there are no values entered I get a undefined method-' for nil:NilClasserror. I triedrescue NoMethodError`, but then it would not calculate the profit. Is there a way to avoid the error and also have my calculation work?
items.rb
class Item < ActiveRecord::Base
def profit_calc
sold_for - bought_for - fees - shipping
end
def self.purchase_total
sum(:bought_for)
end
def self.fee_total
sum(:fees)
end
def self.shipping_total
sum(:shipping)
end
def self.sales_total
sum(:sold_for)
end
def self.profit_total
sum(:sold_for) - sum(:bought_for) - sum(:fees) - sum(:shipping)
end
scope :visible, -> { where(sold: false) }
scope :sold, -> { where(sold: true) }
end
html.erb:
<td><%= number_to_currency(item.profit_calc) %></td>

If profit_calc should return 0 if anything goes wrong or any kind of error occurs, then you can try this:
def profit_calc
sold_for - bought_for - fees - shipping rescue 0
end

Well you could try
def profit_calc
if sold_for then return (sold_for - bought_for - fees - shipping) else return 0 end
end

Related

Rails association scope by method with aggregate

I'm trying to retrieve association records that are dependent on their association records' attributes. Below are the (abridged) models.
class Holding
belongs_to :user
has_many :transactions
def amount
transactions.reduce(0) { |m, t| t.buy? ? m + t.amount : m - t.amount }
end
class << self
def without_empty
includes(:transactions).select { |h| h.amount.positive? }
end
end
class Transaction
belongs_to :holding
attributes :action, :amount
def buy?
action == ACTION_BUY
end
end
The problem is my without_empty method returns an array, which prevents me from using my pagination.
Is there a way to rewrite Holding#amount and Holding#without_empty to function more efficiently with ActiveRecord/SQL?
Here's what I ended up using:
def amount
transactions.sum("CASE WHEN action = '#{Transaction::ACTION_BUY}' THEN amount ELSE (amount * -1) END")END")
end
def without_empty
joins(:transactions).group(:id).having("SUM(CASE WHEN transactions.action = '#{Transaction::ACTION_BUY}' THEN transactions.amount ELSE (transactions.amount * -1) END) > 0")
end

method containing calculation - Rails 4

i just want to calculate the total sum of all active events that users have paid to attend. If you could advise me i could be grateful as i am very unsure. Many thanks
event.rb
has_many :payments
payment
belongs_to :event
in the event.rb i tried the below method but no success
def self.active_events
active_events = live_events.open_events
active_events.all.each do |event|
event.price * event.payments.count
end
end
You can do this simply in following way,
total = 0
Event.live_events.open_events.find_each { |e| total += e.price * e.payments.count }
In Event.rb place it in a method with meaningful name.
This will work for you.
def self.total_price_for_active_events
total = 0
Event.live_events.open_events.find_each { |e| total += e.price * e.payments.count }
total
end
Most optimized way
def self.total_price_for_active_events
Event.live_events.open_events.joins(:payments).sum("events.price")
end
You're off too a good start! Unfortunately, what you have there is only the beginning; you're generating an array that contains the total sum for each event. All that remains is to add them together:
def self.active_events
active_events = live_events.open_events
costs = active_events.all.each do |event|
event.price * event.payments.count
end
costs.reduce(0) do |sum,x|
sum + x
end
end
You could also get real fancy and simply use:
costs.reduce(0, :+)

Virtual Column to count record

First, sorry for my English, I am totally new in ruby on rails even in very basic thing, so I hope you all can help me.
I have table Role and RoleUser
table Role have has_many relationship to RoleUser with role_id as foreign key
in table RoleUser is contain user_id, so I can call it 1 role have many users
and I want is to show all record in Role with additional field in every record called total_users,
total_users is in every record have role_id and count the user_id for every role, and put it in total_users,
I know this is must use the join table, but in rails I absolutely knew nothing about that, can you all give me a simple example how to do that.
and one more, same with case above, can I do for example Role.all and then the total_users in include in that without added it in database? is that use virtual column?
anyone have a good source of link to learn of that
I have following code in model
def with_filtering(params, holding_company_id)
order = []
if params[:sort].present?
JSON.parse(params[:sort]).each do |data|
order << "#{data['property']} #{data['direction']}"
end
end
order = 'id ASC' if order.blank?
if self.column_names.include? "holding_company_id"
string_conditions = ["holding_company_id = :holding_company_id"]
placeholder_conditions = { holding_company_id: holding_company_id.id }
else
string_conditions = []
placeholder_conditions = {}
end
if params[:filter].present?
JSON.parse(params[:filter]).each do |filter|
if filter['operation'] == 'between'
string_conditions << "#{filter['property']} >= :start_#{filter['property']} AND #{filter['property']} <= :end_#{filter['property']}"
placeholder_conditions["start_#{filter['property']}".to_sym] = filter['value1']
placeholder_conditions["end_#{filter['property']}".to_sym] = filter['value2']
elsif filter['operation'] == 'like'
string_conditions << "#{filter['property']} ilike :#{filter['property']}"
placeholder_conditions["#{filter['property']}".to_sym] = "%#{filter['value1']}%"
else
string_conditions << "#{filter['property']} = :#{filter['property']}"
placeholder_conditions["#{filter['property']}".to_sym] = filter['value1']
end
end
end
conditions = [string_conditions.join(' AND '), placeholder_conditions]
total_count = where(conditions).count
if params[:limit].blank? && params[:offset].blank?
data = where(conditions).order(order)
else
data = where(conditions).limit(params[:limit].to_i).offset(params[:offset].to_i).order(order)
end
return data, total_count.to_s
end
And I have follwing code in controllers
def crud_index(model)
data, total = Role.with_filtering(params, current_holding_company)
respond_to do |format|
format.json { render json: { data: data, total_count: total }.to_json, status: 200 }
end
end
My only purpose is to add virtual field called total_users, but i want added it in model and combine it with data in method with_filtering
If you have the models like this:
Class Role < ActiveRecord::Base
has_many :role_users
end
Class RoleUser < ActiveRecord::Base
belong_to :role
end
You could use select and joins to generate summary columns, but all the Role's attributes should be include in group.
roles = Role.select("roles.*, count(role_users.id) as total_users")
.joins(:role_users)
.group("roles.id")
Type those scripts in Rails console, Rails will generate a sql like :
SELECT roles.id, count(role_users.id) as total_users
FROM roles
INNER JOIN role_users
ON roles.id = role_users.role_id
GROUP BY roles.id
Then you can use roles.to_json to see the result. The summary column total_users can be accessed in every member of roles.
And there are many other way can match your requirement. Such as this. There is a reference of counter cache.
My suggestion is after searching, you can test those method by rails console, it's a useful tool.
UPDATE
According to OP's update and comment, seems you have more works to do.
STEP1: move with_filtering class method to controller
with_filtering handle a lot of parameter things to get conditions, it should be handled in controller instead of model. So we can transfer with_filtering into conditions and orders in controller.
class RolesController < ApplicationController
def conditions(params, holding_company_id)
if self.column_names.include? "holding_company_id"
string_conditions = ["holding_company_id = :holding_company_id"]
placeholder_conditions = { holding_company_id: holding_company_id.id }
else
string_conditions = []
placeholder_conditions = {}
end
if params[:filter].present?
JSON.parse(params[:filter]).each do |filter|
if filter['operation'] == 'between'
string_conditions << "#{filter['property']} >= :start_#{filter['property']} AND #{filter['property']} <= :end_#{filter['property']}"
placeholder_conditions["start_#{filter['property']}".to_sym] = filter['value1']
placeholder_conditions["end_#{filter['property']}".to_sym] = filter['value2']
elsif filter['operation'] == 'like'
string_conditions << "#{filter['property']} ilike :#{filter['property']}"
placeholder_conditions["#{filter['property']}".to_sym] = "%#{filter['value1']}%"
else
string_conditions << "#{filter['property']} = :#{filter['property']}"
placeholder_conditions["#{filter['property']}".to_sym] = filter['value1']
end
end
end
return [string_conditions.join(' AND '), placeholder_conditions]
end
def orders(params)
ord = []
if params[:sort].present?
JSON.parse(params[:sort]).each do |data|
ord << "#{data['property']} #{data['direction']}"
end
end
ord = 'id ASC' if ord.blank?
return ord
end
end
STEP2: update action crud_index with conditions and orders to get total_count of Roles.
class AnswersController < ApplicationController
def crud_index(model)
total = Role.where(conditions(params, current_holding_company)).count
if params[:limit].blank? && params[:offset].blank?
data = Role.where(conditions(params, current_holding_company)).order(orders(params))
else
data = Role.where(conditions(params, current_holding_company)).limit(params[:limit].to_i).offset(params[:offset].to_i).order(orders(params))
end
respond_to do |format|
format.json { render json: { data: data, total_count: total }.to_json, status: 200 }
end
end
end
STEP3: update action crud_index to get total_users by every role.
Make sure the two previous steps is pass the test.
class AnswersController < ApplicationController
def crud_index(model)
total = Role.where(conditions(params, current_holding_company)).count
if params[:limit].blank? && params[:offset].blank?
data =
Role.select(Role.column_names.map{|x| "Roles.#{x}"}.join(",") + " ,count(role_users.id) as total_users")
.joins(:role_users)
.group(Role.column_names.map{|x| "Roles.#{x}"}.join(","))
.where(conditions(params, current_holding_company))
.order(orders(params))
else
data =
Role.select(Role.column_names.map{|x| "Roles.#{x}"}.join(",") + " ,count(role_users.id) as total_users")
.joins(:role_users)
.group(Role.column_names.map{|x| "Roles.#{x}"}.join(","))
.where(conditions(params, current_holding_company))
.order(orders(params))
.limit(params[:limit].to_i)
.offset(params[:offset].to_i).order(orders(params))
end
respond_to do |format|
format.json { render json: { data: data, total_count: total }.to_json, status: 200 }
end
end
end
NOTE: step3 may need you to modify conditions and orders method to generate column_name with table_name prefix to avoid column name ambiguous error
If you can make these steps through, I suggest you can try will_paginate to simplify the part of your code about total_count ,limit and offset.
With what you explained, you could do something like this:
class Role < ActiveRecord::Base
has_many :role_users
has_many :users
def total_users
self.users.count
end
end
So you just need to call the total_users method on roles object which should get you what you desire. Something like this:
Role.first.total_users
# this will give you the total users for the first role found in your database.
Hope it helps
You might want to watch this Railscast too:
#app/models/role.rb
Class Role < ActiveRecord::Base
has_many :role_users
has_many :users, -> { select "users.*", "role_users.*", "count(role_users.user_id) as total_users" }, through: :role_users
end
This will allow you to call:
#roles = Role.find params[:id]
#roles.users.each do |role|
role.total_users
end
You can see more about how this works with a question I wrote some time ago - Using Delegate With has_many In Rails?
--
It's where I learnt about Alias columns, which Ryan Bates uses to count certain values:

Rails: comparison of Status with Status failed

I need to fetch all current_user.friends statuses and then sort them by created_at.
class User < ActiveRecord::Base
has_many :statuses
end
class Status < ActiveRecord::Base
belongs_to :user
end
And in the controller:
def index
#statuses = []
current_user.friends.map{ |friend| friend.statuses.each { |status| #statuses << status } }
current_user.statuses.each { |status| #statuses << status }
#statuses.sort! { |a,b| b.created_at <=> a.created_at }
end
current_user.friends returns an array of objects User
friend.statuses returns an array of objects Status
Error:
comparison of Status with Status failed
app/controllers/welcome_controller.rb:10:in `sort!'
app/controllers/welcome_controller.rb:10:in `index'
I had a similar problem, solved with the to_i method, but can't explain why that happens.
#statuses.sort! { |a,b| b.created_at.to_i <=> a.created_at.to_i }
By the way, this sorts in the descending order. If you want ascending order is:
#statuses.sort! { |a,b| a.created_at.to_i <=> b.created_at.to_i }
This error message appears when sort gets a nil returned from <=>. <=> can return -1, 0, 1, or nil, but sort cannot handle nil because it needs all the list elements to be comparable.
class A
def <=>(other)
nil
end
end
[A.new, A.new].sort
#in `sort': comparison of A with A failed (ArgumentError)
# from in `<main>'
One way to debug this kind of error is by checking if the return of your <=> is nil and raising an exception if it is.
#statuses.sort! do |a,b|
sort_ordering = b.created_at <=> a.created_at
raise "a:#{a} b:#{b}" if sort_ordering.nil?
sort_ordering
end
I had a similar problem tonight on a group project. This answer didn't solve it, but what our issue was, someone put other models.new in our def show User controller. For instance...
Class UsersController < ApplicationController
def show
#status = #user.statuses.new
end
This was creating a conflict between the #user.statuses and the #status I was trying to call on the page. I took off the user and just did...
def show
#status = Status.new
end
And that did the trick for me.

How to simplify my model code?

I am new to Rails and I wonder if there's any way to simplify this code from my model:
class Item < ActiveRecord::Base
def subtotal
if price and quantity
price * quantity
end
end
def vat_rate
if price and quantity
0.19
end
end
def total_vat
if price and quantity
subtotal * vat_rate
end
end
end
As far as I know *before_filter* does not work within models?
I'd do:
class Item < ActiveRecord::Base
VAT_RATE = 0.19
def subtotal
(price || 0) * (quantity || 0)
end
def total_vat
subtotal * VAT_RATE
end
end
Personally I would override the getter methods for price and quantity so that they return zero when not set, which allows your other methods to return valid results when no values are set rather than checking that they are and returning nil.
Additionally, creating a method to provide the VAT rate seems a little overkill for what should be a constant. If it isn't a constant then it should probably be stored in the DB so that it can be modified.
Here's a modification of your model based on my thoughts:
class Item < ActiveRecord::Base
VAT_RATE = 0.19
def price
self.price || 0
end
def quantity
self.quantity || 0
end
def subtotal
price * quantity
end
def total_vat
subtotal * VAT_RATE
end
end

Resources