Using ActiveRecord in Ruby On Rails 3
I have 3 models, Customer, Package and location.
The customer logs in, and can view a timeline of locations for his packages.
class Location < ActiveRecord::Base
def find_all_by_customer(customer)
self.joins(:packages => :customer).where("customers.id = ?", customer).select("DISTINCT(locations.id), locations.name, locations.created_at")
end
returns the customer all the locations for all his packages, so they can be displayed on the timeline.
The part I am struggling with, is then displaying the package number/s next to each of these locations. The problem is the package numbers are dependant on both the current_user, and the location.
<% @locations.each do |location| %>
<%= location.name %>
<%= # Want to display all package numbers in th开发者_Go百科is location for the logged in user. %>
<% end %>
You should be able to do something like location.package.reference_number
, if your associations are setup correctly.
精彩评论