Reputation: 650
I am trying to calculate values from existing table column and use it on an external variable.
Let my table columns be as : ["id","unit_price","quantity","extras_1","extras_2"]
I am presenting what i want to do in rails using sql command as reference.
SQL Command:
SELECT unit_price*quantity AS "regular_price",
unit_price*quantity-unit_price*quantity*discount AS "price_after_discount"
FROM order_details;
In Rails Active Record Query i tried same as:
OrderDetail.select('unit_price*quantity AS regular_price,unit_price*quantity-unit_price*quantity*discount AS price_after_discount')
From the above query.i tried sorting based on derived attributes.it worked perfectly.But i cannot see the derived attribute values by querying.
The output i got is without the derived attributes as:
[#<OrderDetail >,#<OrderDetail >]
But i need output as:
[#<OrderDetail regular_price: 43, price_after_discount: 54>,#<OrderDetail regular_price: 54, price_after_discount: 76>]
I tried below query to sort the data.It sorted the data perfectly:
OrderDetail.select('unit_price,quantity,unit_price*quantity AS regular_price,unit_price*quantity-unit_price*quantity*discount AS price_after_discount').order('regular_price desc')
I can access the values using below command:
OrderDetail.select('unit_price,quantity,unit_price*quantity AS extras_1,unit_price*quantity-unit_price*quantity*discount AS extras_2')
above commmand worked because extras_1
and extras_2
are table columns.
But it is working when assigned to existing table column.I need derived attribute to be non-existing table column name.
How can i access a derived attributes values from a record.I can access them by assigning them to an existing table column.But i want the attributes name given how i want irrespective of table columns.
Upvotes: 1
Views: 2126
Reputation: 5528
Your attributes are perfectly accessible. Just call them! :)
As an additional remark I'd suggest you to use a more modern way of writing the query:
class OrderDetail
scope :with_calculated_prices, -> do
regular_price = (arel_table[:unit_price] * arel_table[:quantity])
price_after_discount = (regular_price - regular_price * arel_table[:discount])
select(regular_price.as('regular_price'), price_after_discount.as('price_after_discount'))}
end
price_after_discount is better defined as
price_after_discount = (regular_price * (1 - arel_table[:discount]))
Upvotes: 2
Reputation: 3633
You will not be able to see derived(alias) attributes. But they are present.
OrderDetail.select('unit_price*quantity AS regular_price,unit_price*quantity-unit_price*quantity*discount AS price_after_discount').first.regular_price
Will print regular_price
.
What you see in rails console is output of inspect
method. inspect
method is not implemented to show alias attributes. Hence the confusion arises.
Go through this doc: http://apidock.com/rails/v4.0.2/ActiveRecord/QueryMethods/select
If an alias was specified, it will be accessible from the resulting objects:
Model.select('field AS field_one').first.field_one #=> "value"
Upvotes: 2