Thursday, 5 September 2013

how to retrieve and loop records from 2 tables in rails2.3

how to retrieve and loop records from 2 tables in rails2.3

I have a transport table and students and employee table. In the transport
table I have a column mem_id and mem_type, vehicle_id, date etc.... I am
fetching the records of a vehicle of a date like this..
@mem = []
@tran = Transport.find_all_by_month_and_vehicle(date,vehicle)
tran.each do |t|
@mem << Student.find_by_id(t.mem_id) if t.mem_type=="Student"
@mem<< Employee.find_by_id(t.mem_id) if t.mem_type=="Employee"
end
Now in the @mem I am getting the records of Students and Employees... But
in the view file I am displaying the name, age and id of the students in a
table.
@mem.each do |m|
<tr>
<td><%= link_to m.first_name} %></td>
<td > <%= m.age %></td>
<td id="date"> </td>
<td id="vehicle">vehicle no</td>
</tr>
<%end%>
In the #date and #vehicle_no, I need to display the corresponding records
vehicle_id and date of Transport table. How wil I achieve it.. Do I need
to join both the tables for that.. I am not sure how to do join operation.
OR do I need to loop @tran and insert it into date and vehicle columns..
Please input your thoughts on the best way to do it.

No comments:

Post a Comment