获取 ActiveRecord::Relation 的父级
我有一个已通过协会的部分。我想获取传递给我关联的对象。我该怎么做?我以为这很容易,但我似乎找不到方法。
这是我传递给部分的内容:
<%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
遇到类似的事情,以下工作有效。
ActiveRecord::Association
有一个返回所有者的方法。@foo.bars.proxy_association.owner
=>; @fooRan into something similar and the following worked. The
ActiveRecord::Association
has a method to return the owner.@foo.bars.proxy_association.owner
=>@foo
不知道为什么您反对只将该信息传递到部分中,但如果位置有很多人并且人属于位置,那么您可以只选择该部分中的任何人并调用位置来获取父项。
编辑:在模型上添加一个返回适当关联的
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 exampleperson.parent
would return aLocation
object.Seriously though, there's nothing wrong with just passing the parent into the partial.
我想象在你的 Location 类中你有类似
has_many :people
的东西,而你的 Person 类有类似belongs_to :location
的东西。那么为什么不直接将#parent
别名为每个类的父类呢。然后,您的 Person 类将如下所示:这样您的所有记录都将响应父级,并且您不需要将其传递给部分记录。您需要在家庭课程中做类似的事情。
对此需要注意的是,在部分中调用
parent
将为每条记录从数据库加载父级。当渲染你的部分时,你需要做这样的事情来避免这种情况:I would imagine in your Location class you have something like
has_many :people
, and your Person class has something likebelongs_to :location
. So why not just alias#parent
to the parent of each class. Your Person class would then look like: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: