模型方法或控制器实例变量,Ruby on rails 3

时间:2012-02-25 09:54:42

标签: ruby-on-rails variables model controller

如果我有类似的模型方法:

def favoured_users
 self.followers.limit(5).order("created_at")
end

使用视图块:

<% 5.times do |i| %>
  <li><%= @user.favoured_users[i].name  %></li>
<% end %>

...我会调用favoured_user方法五次,每次都要求5个用户,最后会被调用25个用户吗?

我只是想知道是否应该将favoured_users的结果放在我的控制器的变量中:

@favoured_users = @user.followers.limit(5).order("created_at")

这会减少对服务器的调用吗?

**编辑**

我不确定这是否意味着该值来自缓存,它似乎是(CACHE的bcos,但我不知道这是什么意思),但我没有明确告诉它,做我必须做任何事情以确保它来自缓存:

User Load (0.6ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`followed_id` WHERE `relationships`.`follower_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`followed_id` WHERE `relationships`.`follower_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`followed_id` WHERE `relationships`.`follower_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`followed_id` WHERE `relationships`.`follower_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT COUNT(*) FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`follower_id` WHERE `relationships`.`followed_id` = 1
User Load (0.5ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`follower_id` WHERE `relationships`.`followed_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`follower_id` WHERE `relationships`.`followed_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`follower_id` WHERE `relationships`.`followed_id` = 1 ORDER BY full_name, created_at LIMIT 5
CACHE (0.0ms)  SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `users`.`id` = `relationships`.`follower_id` WHERE `relationships`.`followed_id` = 1 ORDER BY full_name, created_at LIMIT 5

值是从缓存中返回的吗?

编辑我不确定如何从我的视图中访问变量,我按照Sebi的编辑方法进行操作,在我看来,我正在尝试:

<% @user.favoured_followers do %>

  <li><%= @favoured.first.username unless @favoured.first.blank? %></li>
  <li><%= @favoured.second.username unless @favoured.second.blank? %></li>
  <li><%= @favoured.third.username unless @favoured.third.blank? %></li>
  <li><%= @favoured.fourth.username unless @favoured.fourth.blank? %></li>
  <li><%= @favoured.fifth.username unless @favoured.fifth.blank? %></li>

<% end %>

什么都没有被退回?

2 个答案:

答案 0 :(得分:3)

如果查看日志,您可以验证是否每次调用该函数时都会访问缓存。这似乎没有达到缓存,因此加载关注者的查询将为每个用户重复5次。即对于每个@user,您将触发查询5次。因此,在变量中缓存值肯定是一个更好的主意。

修改: 您可以像这样更改模型方法:

def favoured_users
    @favoured ||= self.followers.limit(5).order("created_at")
end

@favoured变量将在第一次调用时创建,然后对于任何后续调用,它将在不触发查询的情况下返回。

您的视图代码应保持不变,不做任何更改。即:

<% 5.times do |i| %>
  <li><%= @user.favoured_users[i].name  %></li>
<% end %>    

答案 1 :(得分:0)

如果我必须这样做......我将在下面进行

内部控制器

@favoured_users = @user.followers.limit(5).order(:created_at)

内部视图

<% @favoured_users do |user| %>
  <li><%= user.name  %></li>
<% end %>
<% (5 - @favoured_users.count).times do%>
 <li>&nbsp;</li>
<% end %>