Rails 关系,至少 3 级
在我的案例中如何真正使用 Rails 关系?
我正在制作一个夜生活网站,只是为了让您了解背景。它有三个模型,我想在一页上加入:地点、相册、照片。
因此,相册
中的照片
效果很好,我在那里使用了带有连接的自定义编码的sql。 但是,当我单击地点
时,我想查看来自地点
模型的信息以及包含示例照片
相册的信息代码>(例如第一个)
现在我有以下内容:
class Place < ActiveRecord::Base
has_many :albums
end
class Album < ActiveRecord::Base
has_many :photos
has_one :place
end
class Photo < ActiveRecord::Base
belongs_to :album
end
我如何在控制器中获取它:
@place = Place.find( params[:id], :include => :albums )
并且我将@place.albums作为带有专辑数据的对象。但我不知道如何深入挖掘以达到照片模型。无论如何我可以:include => :albums因为我在
地点和
album之间没有直接关系,但我将它们放在
album > 中photo`那么需要sql连接吗?
这是一个复杂的问题,但我只是没有完全理解。
感谢您的任何帮助,也许是一个很好的阅读链接!
How can i authentically use rails relationships in my case?
I'm making a nightlife website, just for you to know the context. It has three models, i want to join on one page: Places, Albums, Photos.
So, photos
in albums
work great, i use there a custom-coded sql with joins.
But when i click on a place
i want to see info from place
model and a list of albums
with a sample photo
(first for example)
Now i have the following:
class Place < ActiveRecord::Base
has_many :albums
end
class Album < ActiveRecord::Base
has_many :photos
has_one :place
end
class Photo < ActiveRecord::Base
belongs_to :album
end
And there how i get it in my controller:
@place = Place.find( params[:id], :include => :albums )
and i get @place.albums as an object with albums data. But i dont know how to dig even deeper to reach the
photosmodel. And anywhay i can
:include => :albumsbecause i have no direct relations between
placeand
album, but i have them in
album > photo` so sql join is needed?
That's a complicated question, but i just don`t get it fully.
Thank you for any help, maybe a good link to read!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,地点和相册之间的关联不正确。按照您的设置方式,看起来两者都没有foreign_key。换句话说,它应该是
Album own_to :place
,place_id
在albums
表中。其次,要获取每个相册的第一张照片:
如果您确实愿意,您可以在一行中获取某个地点的第一张相册中的第一张照片:
首先请注意,如果您不这样做,这很容易出现零错误添加条件,所以我实际上不推荐这种方法。但是,如果您要使用它,我只需向 Place 模型添加一个方法:
在您看来:
您也希望将这些东西连接在一起,这样您就不会执行太多查询。 Ogz 巫师的答案涵盖了这一点。
First of all, the association between Place and Album isn't correct. The way you have it setup, it looks like neither would have a foreign_key. In other words, it should be
Album belongs_to :place
, withplace_id
in thealbums
table.Second of all, to get the first photo for each album:
If you really wanted to, you could get the first photo from the first album of a place in one line:
First note that this is prone to nil errors if you don't add in conditionals, so I don't actually recommend this method. But, if you were to use it, I would just add a method to the Place model:
And in your view:
You'll want to join this stuff together as well so you're not performing too many queries. Wizard of Ogz answer covers that.
嵌套包含有一个哈希语法。它也适用于连接。
这是此 http://api.rubyonrails.org/classes/ActiveRecord 的文档/Associations/ClassMethods.html
查看“关联的预加载”标题下的内容
There is a hash syntax for nested includes. It also works for joins.
Here is the documentation for this http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Look under the heading "Eager loading of associations"