I'm trying to search in table policy_logs if it has a pdf.
Here my tables:
|policies|
|id| |name|
1 abc
2 def
3 ghi
|policy_logs|
|id| |name| |policy_id|
1 ab.pdf 1
2 cd.pdf 1
3 ef.pdf 3
4 ij.pdf 3
I'm trying to use all the column policy_id from policy_logs like "id" IN policies table
Here my controller:
@pdf = PolicyLog.find(:all,:select=>['policy_id as pdf'],:group=>['policy_id'])
@policy = Policy.find(:all,:conditions=>['id IN (?)',@pdf ])
Here my models:
class Policy < ActiveRecord::Base
has_many :policy_logs
end
class PolicyLog < ActiveRecord::Base
belongs_to :policy
end
Here my view:
<% @policy.each do |p| %>
<%= p.id %>
<%= p.name %>
<% end %>
This is want I want like result
|id| |policy_id|
1 1
3 3
I tried this but is getting NULL values:
@pdf = PolicyLog.find(:all,:select=>['policy_id as pdf'],:group=>['policy_id'])
@policy = Policy.find(:all,:conditions=>['id IN (?)',@pdf ])
Here is the log:
SELECT policy_id as pdf FROM `policy_logs` GROUP BY policy_id
SELECT * FROM `policies` WHERE (id IN (NULL),(NULL),(NULL),(NULL))
Also tried using array 0:
@pdf = PolicyLog.find(:all,:select=>['policy_id as pdf'],:group=>['policy_id'])
@policy = Policy.find(:all,:conditions=>['id IN (?)',@pdf[0].pdf ])
But I got only one result, here is the log:
SELECT policy_id as pdf FROM `policy_logs` GROUP BY policy_id
SELECT * FROM `policies` WHERE (id IN (1))
Also tried using array 1:
@pdf = PolicyLog.find(:all,:select=>['policy_id as pdf'],:group=>['policy_id'])
@policy = Policy.find(:all,:conditions=>['id IN (?)',@pdf[1].pdf ])
But I got only one result, here is the log:
SELECT policy_id as pdf FROM `policy_logs` GROUP BY policy_id
SELECT * FROM `policies` WHERE (id IN (3))
And Finally i tried
@pdf= PolicyLog.find(:all,:select=>['policy_id as pdf'],:group=>['policy_id'])
@search= Policy.find(:all,:conditions=>['id IN (?)',@pdf.each do |a| { a.pdf} ])
But i got
odd number list for Hash.
unexpected ']', expecting kEND
Maybe there are others ways to search it.
Please somebody can help me, please?
Try like this:
@search = Policy.find(:all,:conditions => {:id => @pdf.map{|p| p.pdf})
Or:
@search = Policy.find(:all,:conditions => {:id => @pdf})