-1

I need to access all values from Database according to id but i am getting the following output.

Output:

all details are #<PaymentVendor:0x1ee5860>
all details are #<PaymentVendor:0x1f02798>

I am explaining my code below.

@rest_ids=[21,22,23]
@rest_ids.each do |ids|
            @pdf_vendor_details = PaymentVendor.where(:id => ids )
            puts "all details are #{@pdf_vendor_details}"
        end

From the above code i have some array of ids(i.e- @rest_ids).Here my requirement is when the loop will execute as per id the record will fetch and store in the variable @pdf_vendor_details in array.If I wanted to display some value in table then i will be able to do that like below.

table.html.erb:

<table>
<tr>
<td>ID</td>
<td>Receipt No</td>
<td>Amount</td>
</tr>
 <% @pdf_vendor_details.each do |details| %>
<tr>
<td><%= details.id %></td>
<td><%= details.Receipt_No %></td>
<td><%= details.Amount %></td>
</tr>
<% end  %>
</table>

But doing this way i can not get any value and unable to display data in table.Please help me to access the data from DB which will store in array to display in table.

rajat_474
  • 346
  • 1
  • 4
  • 15

3 Answers3

1

Try this:

@pdf_vendor_details = PaymentVendor.where(:id => @rest_ids ) #rails 4
#OR
@pdf_vendor_details = PaymentVendor.find_all_by_id(@rest_ids) #rails 3    

@pdf_vendor_details.each do |pdf|
  puts "all details are: ID => #{pdf.id}, Receipt_No => #{pdf.Receipt_No}, Amount=> #{pdf.Amount}"
end

Your table.html.erb will be not changed

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
  • @ Gagan :Ok your line worked but i need another one help.I have one line `@pdf_address=Vendor.where(:v_name => @pdf_vendor_details.v_name , :s_catagory => @pdf_vendor_details.v_catagory )` in this line am getting the following error. `undefined method `v_name' for #` Where v_name always same for all id. – rajat_474 Jun 05 '15 at 06:00
  • you are getting this error bcz you don't have field named `v_name` in `PaymentVendor` table. As you said its same for all then better to pass static. – Gagan Gami Jun 05 '15 at 06:04
  • @ Gagan : No,It has the field name `v_name ` in DB and i have to access the v_name from `@pdf_vendor_details.v_name`. – rajat_474 Jun 05 '15 at 06:09
  • @rajat_474 : try this `@pdf_address=Vendor.where(:v_name => @pdf_vendor_details.map(&:v_name) , :s_catagory => @pdf_vendor_details.map(&:v_catagory))` – Gagan Gami Jun 05 '15 at 06:14
0

You can try

PaymentVendor.where(id: [21, 22, 23])

which will build a SQL statement like

SELECT * FROM payment_vendors WHERE id IN (21, 22, 23)
Aetherus
  • 8,720
  • 1
  • 22
  • 36
  • @ Aetherus : I dont want to set any static value.The id should be dynamic like `@pdf_vendor_details = PaymentVendor.where(:id => ids )`.That ids will contains all three ids. – rajat_474 Jun 05 '15 at 04:57
  • PaymentVendor.where(id: @rest_ids) – Aetherus Jun 05 '15 at 05:02
  • @ Aetherus : No,no record is displaying on the table but this line is giving the output same as `all details are #`. – rajat_474 Jun 05 '15 at 05:26
  • The `#` indicates that you got an instance (the `#`) of the class `PaymentVendor`, and its `object_id` is `0x1f02798`. If you want to see its attributes, call `to_hash` on it. – Aetherus Jun 05 '15 at 05:58
0

I don't see any reason in looping @rest_ids and querying on each iteration

@pdf_vendor_details = PaymentVendor.where(:id => ids )

and in each iteration you are replacing the previous value of @pdf_vendor_details instead of appending to the array.

Instead

@pdf_vendor_details = PaymentVendor.where(id: @rest_ids ) #rails 4

or

@pdf_vendor_details = PaymentVendor.find_all_by_id(@rest_ids) #rails 3

will do. It will send one query to fetch all records of paymentvendor in those ids'.


Update:
If you want to print the contents of the object @pdf_vendor_details in raw form, you could use .inspect or .to_yaml. For example, puts @pdf_vendor_details.inspect, will print the contents of the object. Refer this question, to print contents of object.

@rest_ids=[21,22,23]
@pdf_vendor_details = PaymentVendor.where(id: @rest_ids )
puts "All details are, #{@pdf_vendor_details.inspect}"
Community
  • 1
  • 1
BinaryMee
  • 2,102
  • 5
  • 27
  • 46
  • @ BinaryMee :Actually it was a example(`i.e-@rest_ids=[21,22,23]`).In real case the id also will be dynamic that means `@rest_ids` is going to contain some array of ids.In this case how it will bw resolved. – rajat_474 Jun 05 '15 at 05:01
  • You could dynamically fetch those array in an variable and you could pass it to the condition like @pdf_vendor_details = PaymentVendor.where(id: @rest_ids) – BinaryMee Jun 05 '15 at 05:04
  • @ BinaryMee : By doing this `@pdf_vendor_details = PaymentVendor.where(id: @rest_ids)` and when i checked the this lines o/p like `puts "all details are #{@pdf_vendor_details}"` it is giving the output the same `i.e-all details are #`. – rajat_474 Jun 05 '15 at 05:24
  • If you want to print contents of the object, you could use .inspect or .to_yaml to see the contents. For example, "all details are @pdf_vendor_details.inspect", will give you object's contents! – BinaryMee Jun 05 '15 at 05:36