Store data retrieved as result of find_by_sql method in ruby on rails

135 views Asked by At

I am new to ruby on rails. I am trying to retrieve data from my database and make operation in my rails application. I am stuck while trying to use find_by_sql in my class Activity < ActiveRecord::Base

I have a rates table with structure as

id | Instrument Name | Category | Time_of_usage | Rates
1  | Instrument1     | Internal | Regular       | 25
2  | Instrument2     | Internal | Weekends      | 15  

Here is my sample code of what I am trying to achieve

sql2='SELECT Rates FROM New_billcodes_mock WHERE Category_name=?, Instrument_name=? and Time_zone_name=?'
result2=  find_by_sql[sql2,'Internal','Instrument1',time_zone_name]
database_billrate_value_normal=result2['Rates']

and use the database_billrate_value_normal variable to make my operations.

Please guide me in the correct path if I am wrong.

1

There are 1 answers

4
steve klein On

I'm unclear if the model with the rate table is Activity or NewBillcodeMock or Rate.

If it is Rate, you should be able to do something like this:

database_billrate_value_normal = 
    Rate.where("category_name=? AND 
                instrument_name=? AND 
                time_zone_name=?",
                params[:category_name],
                params[:instrument_name],
                params[:time_zone_name]).pluck[:rates]

... assuming you have a hash params with the required attribute values those are the actual attribute names.

This will return an array of rates which you can then manipulate directly in Ruby as needed.