[英]Using ActiveRecord in Ruby On Rails 3
I have 3 models, Customer, Package and location.我有 3 个模型,客户,Package 和位置。
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.然后,我正在努力解决的部分是在每个这些位置旁边显示 package 编号/秒。 The problem is the package numbers are dependant on both the current_user, and the location.
问题是 package 数字取决于 current_user 和位置。
<% @locations.each do |location| %>
<%= location.name %>
<%= # Want to display all package numbers in this 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.如果您的关联设置正确,您应该能够执行诸如
location.package.reference_number
之类的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.