开发者

limit partial collection from view

开发者 https://www.devze.com 2023-03-11 07:07 出处:网络
I was trying to limit the rendering of partial collection, but I cannot change the controller or model (don\'t ask why, it\'s hard to explaint). So I have to limit it in view and only solution I could

I was trying to limit the rendering of partial collection, but I cannot change the controller or model (don't ask why, it's hard to explaint). So I have to limit it in view and only solution I could come up with is this

def suggested
   @suggested ||= current_user.suggested_friends
end

<%= render :partial => 'layouts/three_panel_widgets/friend', :collection => suggested[0..3] %>

do you have any better ide开发者_运维问答as ?


If you are using rails 3, you can use suggested.limit(4). It will generate a SQL with LIMIT clause. This is a little bit better then using suggested[0..3].

<%= render :partial => 'layouts/three_panel_widgets/friend', :collection => suggested.limit(3) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消