Query that returns multiple results

Asked

Viewed 54 times

0

I have this query MtDispositivo.where("id_conta = ?", "28") that returns to me all accounts that present the ID_CONTA = 28.

Below I have these other Querys that return me same result that are accounts with ID_CONTA such.

 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 return all accounts containing only these IDS to me: 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? Understand the question?


I have a device called Mtdevice where it stores all accounts.

I need to query these ID_CONTAS: 28, 29, 30 e 36 that return are accounts.

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

Instead of going from one to one need a Query where I can get all the results of various IDS at once.

These are IDS: 28, 29, 30 and 36.

  • is using what technologies?

  • I’m using Ruby on Rails. I need to get these ID_CONTA through the Linux terminal. Then create a code snippet where clients can download these IDS in Excel.

2 answers

0

I could not understand the question very well, but apparently the sql operator IN helps you.

select * from tabela where id_conta IN (28, 29, 30, 36)

0

Only by complementing the above answer, you can use IN.

To generate the download, Voce will have to use csv. I own a system that handles 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 << Voce you will put the name of the columns, and in the second the values that will appear in each row in the respective columns.

That code should be on your model. After that, in your Voce controller you have to define a function that will generate 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. Once done, simply generate a button in your view that calls the generate_csv path. Remember to adjust the parameters according to your need

Browser other questions tagged

You are not signed in. Login or sign up in order to post.