开发者

Using ActiveRecord in Ruby On Rails 3

开发者 https://www.devze.com 2023-03-09 12:24 出处:网络
I have 3 models, Customer, Package and location. The customer logs in, and can viewa timeline of locations for his packages.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号