rails paperclip no direct access through sql query from another model

风流意气都作罢 提交于 2019-12-21 20:44:00

问题


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

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 %>

listing and category have the habtm relation


回答1:


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])


来源:https://stackoverflow.com/questions/19756998/rails-paperclip-no-direct-access-through-sql-query-from-another-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!