I am totally new to Ruby, and Rails. Currently, I am using helper methods. How can I write the same code as this in my Model 'User' so as to access all these variables from controller and view?
Writting code this way in helper is 100% functional:
module HomeHelper
def init(user_id)
@friends = Array.new
@followers = Array.new
@user = User.find_by_id(user_id) #Get User
@friends = @user.users #Get all his friends
#
@statuses = Array.new #
@friends.each do |friend| #
@statuses += friend.statuses #Get all statuses for 'a' friend, then loop
end #
@statuses += @user.statuses #
@statuses = @statuses.sort_by {|status| status.created_at}.reverse!
@friendsof = Array.new
@filtered_friendsof = Array.new
@friends.each do |friend|
@friendsof += friend.users
end
@friendsof.each do |friendof|
unless (@friends.include?(friendof))
if @user != friendof
@filtered_friendsof << friendof
end
end
end
end
@filtered_friendsof = @filtered_friendsof.uniq
end
Controller
class HomeController < ApplicationController
def index
@user_id=3
end
end
Model:
class User < ActiveRecord::Base
has_many :statuses
has_and_belongs_to_many(:users,
:join_table => "user_connections",
:foreign_key => "user1_id",
:association_foreign_key => "user2_id")
#has_many :user_connections
end