是否有 Rails 方式或 gem 可以将相关条目从 HABTM 获取到同一对象?
好吧,我有一个包含图像的表,每个图像都通过连接表用 HABTM 关系标记。
我想要做的是通过匹配具有相同标签的图像,在正在显示图像的页面中显示相关图像。
可以使用当前连接表有效地完成此操作,还是应该在每次创建新图像时在另一个表中的图像之间生成连接?
Well, I have a table with images, each image is tagged with a HABTM relation through a join table.
What I want to do is show related images in the page the image is being shown, by matching images with the same tags.
Can this be done efficiently with the current join table or I should generate joins between images in another table each time a new image is created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您考虑改用
has_many :items, :through => :link_table
它已成为新的标准,并且是一种很好的方法,因为您可以“融入”更多的灵活性,并且该结构也可以轻松扩展和增长,而无需进行重大返工,例如添加新属性非常容易。 “新属性”通常是日期字段。
因此,您可以添加常规时间戳字段(创建和更新),人们还发现其他日期字段(如“completed_on”、“authorized_on”、“termerated_on”、“activated_on”、“sold_on”等)很有用,具体取决于应用程序的使用案例。
您仍然会看到很多 HABTM 的示例,而且它仍然有效。当然,在某些情况下,使用它可能仍然有意义,但正如 HMT 所做的那样,KISS 表示使用“一种方式”。
I recommend you consider switching to using
has_many :items, :through => :link_table
It's become the new standard and it's a great approach because you get more flexibility 'baked in' and the structure is also ready for easy expansion and growth without major rework, e.g. adding new attributes is very easy. 'new attributes' are frequently date fields.
So you can add the regular timestamps fields (created and updated) and people also find other date fields like 'completed_on', 'authorized_on', 'terminated_on', 'activated_on', 'sold_on', etc. useful, depending on the application use cases.
You will still see a lot of examples of HABTM and it does still work. There are certainly cases where it may still make sense to use it but as HMT does them anyway, KISS says use 'one way'.