获取 ActiveRecord::Relation 的父级

发布于 2025-01-08 11:35:28 字数 760 浏览 0 评论 0原文

我有一个已通过协会的部分。我想获取传递给我关联的对象。我该怎么做?我以为这很容易,但我似乎找不到方法。

这是我传递给部分的内容:

    <%= render :partial => 'shared/records_table', :locals => {:parent => @location, :records => @location.people} %>

这是我想传递给部分的内容:

    <%= render :partial => 'shared/records_table', :locals => {:records => @location.people} %>

还有这样的实例:

    <%= render :partial => 'shared/records_table', :locals => {:parent => @time, :records => @time.families} %>

并且:

    <%= render :partial => 'shared/records_table', :locals => {:parent => @car, :records => @car.families} %>

我不知道关联或父级的类。

I have a partial that has been passed an association. I want to get the object that passed me the association. How do I do this? I thought it would be easy but I cannot seem to find the method.

Here is what I pass to the partial:

    <%= render :partial => 'shared/records_table', :locals => {:parent => @location, :records => @location.people} %>

Here is what I would like to pass to the partial:

    <%= render :partial => 'shared/records_table', :locals => {:records => @location.people} %>

There are also instances like this:

    <%= render :partial => 'shared/records_table', :locals => {:parent => @time, :records => @time.families} %>

And:

    <%= render :partial => 'shared/records_table', :locals => {:parent => @car, :records => @car.families} %>

I do not know the class of the association or the parent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往事风中埋 2025-01-15 11:35:28

遇到类似的事情,以下工作有效。 ActiveRecord::Association 有一个返回所有者的方法。

@foo.bars.proxy_association.owner =>; @foo

Ran into something similar and the following worked. The ActiveRecord::Association has a method to return the owner.

@foo.bars.proxy_association.owner => @foo

著墨染雨君画夕 2025-01-15 11:35:28

不知道为什么您反对只将该信息传递到部分中,但如果位置有很多人并且人属于位置,那么您可以只选择该部分中的任何人并调用位置来获取父项。

编辑:在模型上添加一个返回适当关联的 parent 方法。例如 person.parent 将返回一个 Location 对象。

但说真的,将父级传递给局部并没有什么问题。

Not sure why you're opposed to just passing that information into the partial, but if location has many people and person belongs to location, then you can just take any of the people in that partial and call location to get the parent.

edit: Add a parent method on your models that returns the appropriate association. For example person.parent would return a Location object.

Seriously though, there's nothing wrong with just passing the parent into the partial.

岁月蹉跎了容颜 2025-01-15 11:35:28

我想象在你的 Location 类中你有类似 has_many :people 的东西,而你的 Person 类有类似 belongs_to :location 的东西。那么为什么不直接将 #parent 别名为每个类的父类呢。然后,您的 Person 类将如下所示:

belongs_to :location
alias_method :parent, :location

这样您的所有记录都将响应父级,并且您不需要将其传递给部分记录。您需要在家庭课程中做类似的事情。

对此需要注意的是,在部分中调用 parent 将为每条记录从数据库加载父级。当渲染你的部分时,你需要做这样的事情来避免这种情况:

render :partial => 'shared/records_table', :locals => {:records => @location.people.includes(:location)}

I would imagine in your Location class you have something like has_many :people, and your Person class has something like belongs_to :location. So why not just alias #parent to the parent of each class. Your Person class would then look like:

belongs_to :location
alias_method :parent, :location

That way all your records will respond to parent and you won't need to pass it in to the partial. You'd need to do something similar in your Family class.

A caveat on this is that calling parent in the partial will load the parent from the database for each record. When rendering your partial you would need to do something like this to avoid that:

render :partial => 'shared/records_table', :locals => {:records => @location.people.includes(:location)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文