ruby on rails - Using a method within model, calling it from view -


i have update model belongs users. show of 1 user's friends' updates, doing like:

update.where("user_id" => [array_of_friend_ids]) 

i know "right" way of doing things create method create above array. started writing method it's half-working. have in user model:

  def self.findfriends(id)     @friendarray = []     @registered_friends = friend.where("user_id" => id)     @registered_friends.each |x|       @friendarray << x.friend_id     end      return @friendarray   end  

i doing entire action in view with:

<% @friendinsert = user.findfriends(current_user.id) %> <% @friendarray  = [] %> <% @friendarray << @friendinsert %> <%= @friendarray.flatten! %> 

then i'm calling update.where("user_id" => @friendarray) works. i'm doing things in hacky way here. i'm bit confused when rails can "see" variables models , methods in view. what's best way go inserting array of ids find updates, since i'm not supposed use logic in view itself?

mattharick right using associations. should use associations question mentioned in description of question. if come question @ title of question;

let's have user model.

these 2 methods different:

def self.testing   puts "i'm testing" end 

and other 1 is:

def testing   puts "i'm testing" end 

pay attention self keyword. self keyword makes method class method. can call controllers or views like: user.testing.

but 1 out testing instance method. can called like:

u = user.last u.testing 

second 1 gives possibility use attributes of 'instance' inside model.

for example, can show name of instance in method this?

def testing   puts "look, i'm showing instance's name is: #{name}" end 

these powerful stuff.

practise on them.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -