Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:08:08+00:00 2026-06-12T18:08:08+00:00

I am totally new to Ruby, and Rails. Currently, I am using helper methods.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:08:09+00:00Added an answer on June 12, 2026 at 6:08 pm

    Home controller:

    class HomeController < ApplicationController
      def index
        @user = User.find(3)
      end 
    end
    

    User model:

    class User < ActiveRecord::Base
      has_many :statuses
      has_and_belongs_to_many :friends,
        :class_name => 'User'
        :join_table => "user_connections",
        :foreign_key => "user1_id",
        :association_foreign_key => "user2_id"
    
      def combined_statuses
        (friends.map(&:statuses) + statuses).flatten.
          sort_by {|status| status.created_at}.reverse!
      end
    end
    

    Now, you don’t need your helper method and in your view you can use:

    @user.friends # instead of @friends
    @user.combined_statuses # instead of @statuses
    

    I’ll let you figure out the rest, but I hope you get the general idea of pushing the logic into the model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm totally new to rails (and, of corse, to ruby). Rails by default, when
Disclaimer : I am totally new to Ruby on Rails but planning learning it.
Totally new to programming so kindly excuse the silly question. I have this URL
im totally new when it comes to trying to make HTML code. I was
I'm totally new to this WCF Services thing so any guidelines would be greatly
I am absolutely and totally new to rails, so the answer is probably very
I´m totally new to Powershell and wanted to write a script that deletes all
This is a totally new area for me so please be patient. I want
I'm totally new to Ruby but not to programming. All I did was going
I am totally new to Android . I am using the book Hello, Android.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.