Rails 关系,至少 3 级

发布于 2024-12-09 07:12:06 字数 838 浏览 0 评论 0原文

在我的案例中如何真正使用 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 thephotosmodel. And anywhay i can:include => :albumsbecause i have no direct relations betweenplaceandalbum, but i have them inalbum > 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 技术交流群。

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

发布评论

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

评论(2

风启觞 2024-12-16 07:12:06

首先,地点和相册之间的关联不正确。按照您的设置方式,看起来两者都没有foreign_key。换句话说,它应该是 Album own_to :placeplace_idalbums 表中。

其次,要获取每个相册的第一张照片:

<% @place.albums.each do |album| %>
  Sample Photo: <%= image_tag(album.photos.first.url) %>
<% end %>

如果您确实愿意,您可以在一行中获取某个地点的第一张相册中的第一张照片:

<%= image_tag(@place.albums.first.photos.first.url) %>

首先请注意,如果您不这样做,这很容易出现零错误添加条件,所以我实际上不推荐这种方法。但是,如果您要使用它,我只需向 Place 模型添加一个方法:

def first_photo
  albums.first.photos.first
end

在您看来:

<%= image_tag(@place.first_photo.url) %>

您也希望将这些东西连接在一起,这样您就不会执行太多查询。 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, with place_id in the albums table.

Second of all, to get the first photo for each album:

<% @place.albums.each do |album| %>
  Sample Photo: <%= image_tag(album.photos.first.url) %>
<% end %>

If you really wanted to, you could get the first photo from the first album of a place in one line:

<%= image_tag(@place.albums.first.photos.first.url) %>

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:

def first_photo
  albums.first.photos.first
end

And in your view:

<%= image_tag(@place.first_photo.url) %>

You'll want to join this stuff together as well so you're not performing too many queries. Wizard of Ogz answer covers that.

你与清晨阳光 2024-12-16 07:12:06

嵌套包含有一个哈希语法。它也适用于连接。

@place = Place.find( params[:id], :include => {:albums => :photos} )

这是此 http://api.rubyonrails.org/classes/ActiveRecord 的文档/Associations/ClassMethods.html
查看“关联的预加载”标题下的内容

There is a hash syntax for nested includes. It also works for joins.

@place = Place.find( params[:id], :include => {:albums => :photos} )

Here is the documentation for this http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Look under the heading "Eager loading of associations"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文