2010-12-16 7 views
1

이제 비디오의 축소판 파일을 다루고 있습니다. 나는 다형성 연관 사용하려면 :다형성 연관에 대한 질문

class Picture < ActiveRecord::Base 
    belongs_to :pictureable, :polymorphic => true 
end 


class Video < ActiveRecord::Base 
    belongs_to :videoable, :polymorphic => true 
    has_many :pictures, :as => :pictureable # I want to use has_one :picture, :as => :picturealbe, it make sense to let each video have a single picture as thumbnail but it doesn't work for me, video.picture return a nil 
end 

class Drummer < ActiveRecord::Base 
    has_many :videos,:as => :videoable 
    has_many :pictures, :as => :pictureable 
end 

을하고 나는 그것이 썸네일

<% @drummer.videos.each do |v|%> 
<li> 
<%= link_to video_path(v) do %> 
<%= image_tag(v.pictures.find(1).url) %> 
<h4><%= v.title %></h4> 
<h5><%= v.desc %></h5> 
<h6>event place</h6> 
<% end %> 
</li> 
<% end %> 

의 내가 개발 로그 파일에서이 오류가있어 모든 비디오를 표시하려면이 옵션을 사용하려면 :

ActionView::Template::Error (Couldn't find Picture with ID=1 [WHERE ("pictures".pictureable_id = 3 AND "pictures".pictureable_type = 'Video')]): 
    92: <% @drummer.videos.each do |v|%> 
    93: <li> 
    94: <%= link_to video_path(v) do %> 
    95: <%= image_tag(v.pictures.find(1).url) %> 
    96: <h4><%= v.title %></h4> 
    97: <h5><%= v.desc %></h5> 
    98: <h6>event place</h6> 
    app/views/drummers/show.html.erb:95:in `block (2 levels) in _app_views_drummers_show_html_erb___754323413_3886850_892596806' 
    app/views/drummers/show.html.erb:94:in `block in _app_views_drummers_show_html_erb___754323413_3886850_892596806' 
    app/views/drummers/show.html.erb:92:in 

`_app_views_drummers_show_html_erb___754323413_3886850_892596806 '

내 솔루션이 전혀 우아하지 않지만 더 나은 아이디어를 내놓을 수 없습니다. 제안 사항이 있습니까? 덕분에 모두

답변

1

당신은 주어진 영상과 관련된 및 ID 1을 가지고 있으며, 때때로 오류와 붙어 사진을 찾기 위해 노력할 것입니다

<%= image_tag(v.pictures.first.url) %> 

같은 이미지 태그를 렌더링 할 때 어떤 그러한 기록이 존재합니다.

+0

와우, 그건 의미가 있습니다. 감사 – mko

1

나는 또한 레일 초보자 해요,하지만 난 ... 최근에 비슷한 문제가 있었다 이러한 라인을 따라 뭔가가 나를 위해 일한 :이 도움이

class Video < ActiveRecord::Base 
    belongs_to :videoable, :polymorphic => true 
    has_one :picture, :foreign_key => :pictureable_id 
end 

희망! 명시 적으로 v.pictures.find(1)을 말할 때

+0

고마워 .lainie 곧 코드를 사용해 보겠습니다 :-) – mko