Rails 范围为 has_many :through 访问额外数据 [英] Rails Scoping For has_many :through To Access Extra Data

查看:39
本文介绍了Rails 范围为 has_many :through 访问额外数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在 Rails 中,您可以向 has_many :through 连接模型添加额外的属性

我想从连接模型中提取额外的数据 &用它填充视图.我通过直接引用连接模型来实现这一点,但我想将相关的连接模型数据附加到当前项目,如下所示:

#app/models/message.rb#图片has_many :image_messages, :class_name =>'图像消息'has_many :images, :class_name =>'图像', :through =>:image_messages, 依赖: :destroy#app/views/messages/show.html.erb<%=@message.image_messages.first.caption %>#->作品#app/views/messages/show.html.erb<%=@message.images.first.caption %>#->我想发生什么

具体来说,我需要能够调用特定图像的 caption,其中 caption 存储在 image_messages 连接模型中,像这样:

#image_messages 连接表身份证 |message_id |image_id |标题 |created_at |更新时间

据我所知,可能可以为此使用范围,但我不确定

任何帮助将不胜感激!我想我会问这个问题,因为它是你不经常得到的问题之一!

解决方案

我确实找到了解决方案

神奇之处在于 SQL SELECT 语句,我设法在 这个 RailsCast

特别是,除了标准的 SELECT * FROM table,您还可以定义另一个函数,并使用 AS 函数

所以我得到的是这个代码:

#Imageshas_many :image_messages, :class_name =>'图像消息'has_many :图像,->{ select("#{Image.table_name}.*, #{ImageMes​​sage.table_name}.caption AS caption") }, :class_name =>'图像', :through =>:image_messages, 依赖: :destroy

这允许您将图像的标题"附加到 Image 对象,从而允许您调用 @image.caption -- 它工作得非常出色!

As we know, in rails you can add extra attributes to your has_many :through join model

I'd like to extract the extra data from the join model & populate the view with it. I have got this working by referencing the join model directly, but I'd like to append the relevant join-model data to the current item, like this:

#app/models/message.rb
    #Images
    has_many :image_messages, :class_name => 'ImageMessage'
    has_many :images, :class_name => 'Image', :through => :image_messages, dependent: :destroy

   #app/views/messages/show.html.erb
   <%= @message.image_messages.first.caption %> #-> works

   #app/views/messages/show.html.erb
   <%= @message.images.first.caption %> #-> what I'd like to happen

Specifically, I need to be able to call the caption of a particular image, with caption being stored in the image_messages join model, like this:

#image_messages join table
id | message_id | image_id | caption | created_at | updated_at

From what I've seen, might be able to use a scope for this, but I'm not sure

Any help will be appreciated! I figured I'd ask the question because it's one of those which you don't get that often!

解决方案

I actually found the solution to this

The magic lies in the SQL SELECT statement, which I managed to find out about on this RailsCast

Specifically, alongside the standard SELECT * FROM table, you can define another function, and attach it to the original query with the AS function

So what I got was this code:

#Images
    has_many :image_messages, :class_name => 'ImageMessage'
    has_many :images, -> { select("#{Image.table_name}.*, #{ImageMessage.table_name}.caption AS caption") }, :class_name => 'Image', :through => :image_messages, dependent: :destroy

This allows you to attach the "caption" for the image to the Image object, allowing you to call @image.caption -- it works brilliantly!

这篇关于Rails 范围为 has_many :through 访问额外数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆