Rails Paperclip:另一个模型无法通过SQL查询直接访问

3

如果尝试通过SQL查询从另一个模型中访问paperclip图像以在其视图中显示,那么它不会显示图像。

我尝试了类似于这样的操作,从一个类别控制器接收表单参数,并通过选择框在索引页面中展示:

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

在展示页面中,我无法从类别控制器访问列表模型中的 paperclip 属性。
category show page      

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

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

<% end %>

列表和类别之间存在habtm关系

1个回答

0

这是一篇旧帖子,但我遇到了相同的问题,使用正确的查询和您已经拥有的相同循环很容易解决,但我的问题出现在 Rails 4 中。

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接