简体   繁体   English

通过父模型访问姐妹孩子的属性

[英]Access the attributes of a sister child via the parent model

So I've been looking all over and can't seem to find the correct conceptual explanation/technical process for this, and quite possibly am approaching it incorrectly. 因此,我一直到处寻找,似乎无法为此找到正确的概念解释/技术过程,并且很可能会错误地使用它。

I have 2 child models, connected by the same parent, I want to access the attributes of CHILD B from an Index view on CHILD A :THROUGH an instance of the parent. 我有2个子模型,由同一父模型连接,我想从CHILD A的索引视图访问CHILD B的属性:通过父模型的实例。

High level overview 高层概述

    Property
   /        \
Order      Owner  

Basically I have an index page showing all the open Orders , each Order must have a Property (parent) and every Property has an owner. 基本上,我有一个索引页面,显示所有未结Orders ,每个Order必须有一个Property (父对象),每个Property都有一个所有者。 So I'm in the Orders index page and I want to see the first_name of the Property's Owner , how can I access that? 因此,我在“ Orders索引页面中,我想查看Property's Ownerfirst_name ,如何访问它?

If I do something like: 如果我做类似的事情:

@order.property.owners.first_name

I get a an error undefined method , however if I do: 我收到一个错误undefined method ,但是如果这样做:

@order.property.owners

I don't receive an error but rather: 我没有收到错误,而是:

    #<Owner::ActiveRecord_Associations_CollectionProxy:0x007f8a85368988>

shows up in my view, along with all the other correct information. 在我看来与所有其他正确信息一起出现。

Models 楷模

Property
has_many :orders
has_many :owners
accepts_nested_attributes_for :owners

Owner
belongs_to :property

Orders
belongs_to :property
has_many :owners, through: :property
accepts_nested_attributes_for :owners

Orders Controller 订单控制器

  def open_orders
@open_orders = Order.where(status: "Open").paginate(page: params[:page], per_page: 15)
###@orders = Order.all
###@properties = Property.find(@orders.property.id)
###@owners = Owner.find(@properties.owners.id)
render 'orders/open'

end 结束

^ Commented out stuff I was trying but was not working, including it incase I'm just a little bit off somewhere. ^注释掉了我正在尝试但无法正常工作的东西,包括它,以防我在某处有点距离。

Open_orders View Open_orders视图

<% @open_orders.each do |order| %>
  <tr>
    <td><%= order.client_id %></td>
    <td><%= order.order_number %></td>
    <td><%= order.property.address1 %></td>
    <td><%= order.property.owners %></td>
    <td><%= order.order_update ? "Yes" : "No" %></td>
    <td><%= order.status %></td>
    <td><%= order.task %></td>
    <td><%= order.created_at %></td>
    <td><%= link_to fa_icon("edit"), edit_property_order_path(order, order.property.id), class: "m-r-xs", remote: true, data: { 'data-toggle' => "modal", 'data-target' => '#orders-modal'} %></td>
  </tr>

As per your model definition, an order has multiple owners ..thus you are getting active record collection of owners instead of single object. 根据您的模型定义,一个order具有多个 owners因此,您将获得所有者的有效记录集合,而不是单个对象。

There are 2 ways to handle this, 有两种处理方法,

  1. Loop through the collection and get the names 遍历集合并获取名称
  2. Pluck all the owner names, which is I think you are looking for 摘下所有所有者的名字,这是我认为您正在寻找的

You must preload the owners to avoid n+1 queries 您必须预先加载所有者,以避免n + 1个查询

In your controller 在您的控制器中

@open_orders = Order.where(status: "Open").includes(:owners).paginate(page: params[:page], per_page: 15)

In your view 在您看来

<td><%= order.owners.pluck(:first_name).join(',') %></td>

Update 更新

To get the full_name of the owner, you can define a method in your owner.rb as 要获得full_name主人的,你可以在你定义一个方法owner.rb作为

def full_name
  "#{first_name} #{last_name}"
end

In your view 在您看来

<td><%= order.owners.map(&:full_name).join(',') %></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM