我有以下兩種模式:獲得一個嵌套模式的伯爵在控制器
photoalbums
has_many:photos
photos
belongs_to:photoalbums
當我告訴所有的photoalbums的名單,我想也說有多少張照片在相冊中存在:
控制器:
def index
@photoalbums = PhotoAlbum.all
end
查看:
<%# Get the number of photos per this album %>
<% @photos = Photo.find_by_photo_album_id(photoalbum.id) %>
<li><%= @photos.count %> Photos</li>
以上不允許我在視圖中做@photos.count
或@photos.record
。
有沒有更好的方法在控制器中做到這一點?我想在控制器中可能是include(:photos)
?
謝謝!
你說的 「不容許」 是什麼意思?如果你去腳本/控制檯並輸入相同的東西,@photos的價值是多少?你爲什麼不使用photoalbum.photos.count? – 2010-10-23 06:05:09