Query to return multiple results

0

I have this Query MtDispositivo.where("id_conta = ?", "28") which returns all the accounts that have ID_CONTA = 28 .

Below I have these other Queries that return me same result that are accounts with ID_CONTA tal.

 MtDispositivo.where("id_conta = ?", "29")

 MtDispositivo.where("id_conta = ?", "30")

 MtDispositivo.where("id_conta = ?", "36")

I need to do a Query with all these IDS_CONTAS to return all accounts that contain only these IDs: 28 , 29 , 30 > and 36 .

And then I need to add a Download session, where these accounts will be downloaded in Excel.

Can you help me? Did you understand the question?

I have a Device called MtDispository where it stores all accounts.

I need to make a Query with these ID_CONTAS: 28, 29, 30 e 36 that return these accounts.

When I made MtDispositivo.where("id_conta = ?", "29") it returns only accounts that contain ID_CONTA 29 .

Instead of doing one-on-one rendering of a Query where I can get all the results from multiple IDS instead.

They are the IDS: 28, 29, 30 and 36.

    
asked by anonymous 29.06.2015 / 17:15

2 answers

0

You can not understand the question very well, but apparently the sql IN operator helps you.

select * from tabela where id_conta IN (28, 29, 30, 36)
    
29.06.2015 / 17:19
0

By completing the above answer, you can use IN.

To generate the download, you will have to use csv. I own a system that deals with clients. The code to generate his csv is below:

#Generate the CSV file
def self.to_csv(distributor_id)
    sql = "SELECT * FROM customer_breakdown WHERE distributor_id = '#{distributor_id}' ORDER BY reference_date DESC"
    @customers = {}
    @customers = CustomerBreakdown.find_by_sql(sql)
    CSV.generate do |csv|
        csv << ["iccid", "unique_id", "total_revenue", "call_usage", "sms_usage", "data_usage", "packages_purchased", "forwarding_numbers", "vouchers_applied", "reference_date"]
        @customers.each do |customer|
            csv << [customer.iccid, customer.unique_id, customer.total_revenue, customer.call_usage, customer.sms_usage, customer.data_usage, customer.packages_purchased, customer.forwarding_numbers, customer.vouchers_applied, customer.reference_date]
        end
    end
end

In the first csv you will put the name of the columns, and in the second the values that will appear in each row in the respective columns.

This code should go in your model. After that, in your controller you have to define a function that will generate the csv.       def generate_csv

respond_to do |format|
  format.csv{ send_data CustomerBreakdown.to_csv(current_user.id) }
end

end

In this case the answer to this call will be in csv format. Done this you just generate a button in your view that calls the generate_csv path. Remember to adjust the parameters according to your needs.

    
29.06.2015 / 21:53