使用 where 和 select 子句时,如何 select rails 中的图像列

How do I select the image column in rails when using where & select clause

我一直在使用回形针上传和获取图像url。我的问题是如何 select 徽标列? 例如:用户 table 结构 id ,name, logo_file_name, logo_content_type + 更多列 当我 u = User.find(1).logo 得到很棒的结果时。现在当我做 u = User.select('name', 'logo').where('something') 时收到错误消息说没有像 u.logo 这样的列,这对我来说很清楚,因为没有列,这就是它给出的原因我的错误,但是如何在以后的情况下获取图像 url。

如果您不包含徽标列,ActiveRecord 将无法对其进行序列化。

因此,如果您希望 User 实例响应徽标,则只需将其包含在您的 select 语句中即可。

我自己解决了这个问题,这里是一个快速的答案,需要 select 由回形针生成的所有 4 列,例如 users.logo_file_name, users.logo_content_type, users.logo_file_size, users.logo_updated_at

所以当我们这样做时

u = User.select('logo_file_name, logo_content_type, logo_file_size, logo_updated_at').where(id: 29) 然后做 u.first.logo 获取图像 URL.

谢谢!