Note that there are some explanatory texts on larger screens.

plurals
  1. POModel method or controller instance variable, Ruby on rails 3
    primarykey
    data
    text
    <p>If i have a model method like:</p> <pre><code>def favoured_users self.followers.limit(5).order("created_at") end </code></pre> <p>with a view block like:</p> <pre><code>&lt;% 5.times do |i| %&gt; &lt;li&gt;&lt;%= @user.favoured_users[i].name %&gt;&lt;/li&gt; &lt;% end %&gt; </code></pre> <p>...would I be calling the favoured_user method five times and each time asking for the 5 users, ending up with like 25 users being called?</p> <p>I'm just wondering if I should put the result of favoured_users in a variable in my controller instead:</p> <pre><code>@favoured_users = @user.followers.limit(5).order("created_at") </code></pre> <p>Would that be less calls to the server?</p> <p>** EDIT **</p> <p>I'm not sure if this means the value is coming from the cache, it appears it is (bcos of the CACHE, but I dont know thats what it means), but I haven't explicitly told it to, do I have to do anything to make sure it does come fro the cache:</p> <pre><code>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 </code></pre> <p>Is the value returning from the cache?</p> <p><strong>EDIT</strong> I'm not sure how to access the variable from my view, I have the method as per Sebi's edit and in my view I am trying to:</p> <pre><code>&lt;% @user.favoured_followers do %&gt; &lt;li&gt;&lt;%= @favoured.first.username unless @favoured.first.blank? %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= @favoured.second.username unless @favoured.second.blank? %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= @favoured.third.username unless @favoured.third.blank? %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= @favoured.fourth.username unless @favoured.fourth.blank? %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= @favoured.fifth.username unless @favoured.fifth.blank? %&gt;&lt;/li&gt; &lt;% end %&gt; </code></pre> <p>Nothing is being returned?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload