I have an Update model which belongs to users. To show all of one user's friends' Updates, I am doing something like:
Update.where("user_id" => [array_of_friend_ids])
I know the "right" way of doing things is to create a method to create the above array. I started writing the method but it's only half-working. Currently I have this in my user model:
def self.findfriends(id)
@friendarray = []
@registered_friends = Friend.where("user_id" => id)
@registered_friends.each do |x|
@friendarray << x.friend_id
end
return @friendarray
end
I am doing the entire action in the view with:
<% @friendinsert = User.findfriends(current_user.id) %>
<% @friendarray = [] %>
<% @friendarray << @friendinsert %>
<%= @friendarray.flatten! %>
Then I'm calling Update.where("user_id" => @friendarray) which works. But obviously I'm doing things in a very hacky way here. I'm a bit confused as to when Rails can "see" certain variables from models and methods in the view. What's the best way to go about inserting an array of IDs to find their Updates, since I'm not supposed to use much logic in the view itself?