Rails回形针无法通过其他模型的sql查询直接访问 [英] rails paperclip no direct access through sql query from another model

查看:55
本文介绍了Rails回形针无法通过其他模型的sql查询直接访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果试图从另一个模型访问回形针图像以通过sql查询在其视图中显示,它将不会显示图像。

if tried to access paperclip images from another model to show in its view through a sql query, it wont show the images.

我从a尝试了类似的操作类别控制器wch通过选择框在索引页中的表单中获取参数。

i tried something like this from a category controller wch takes in params from a form, in the index page, thru select box.

category controller

def show

@category = Category.find_by_sql ["select distinct l.* from listings l , categories c, categories_listings cl where c.id = cl.category_id and l.id = cl.listing_id and c.id in (?,?)" ,  params[:c][:id1] , params[:c][:id2]]

end

在显示页面中,我无法从类别控制器访问列表模型中的回形针属性。

in the show page, i cannot access the paperclip attribute wch is in the listing model, from the category controller.

category show page      

<% @category.each do |c| %>

<%= c.place %>
<%= image_tag c.placeholder.url(:thumb) %>

<% end %>

列表和类别具有habtm关系

listing and category have the habtm relation

推荐答案

这是一篇过时的文章,但是我遇到了同样的问题,并且可以通过正确的查询和已经存在的相同循环轻松解决,但是我的问题出在Rails 4中。

This is an old post but I had run into the same problem and was an easy fix with the correct query and the same loop you already have but my issue was in Rails 4.

对于那些将来遇到此问题的人,请尝试使用

For those who have this issue in the future try using

Category.where(:id => params[:id])

这篇关于Rails回形针无法通过其他模型的sql查询直接访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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