Rails:为视图创建变量的更好方法

时间:2012-10-13 14:12:23

标签: ruby-on-rails ruby model helper

我是Ruby和Rails的新手。目前,我正在使用帮助方法。如何在我的模型“用户”中编写与此相同的代码,以便从控制器和视图访问所有这些变量?

以这种方式在帮助程序中编写代码是100%功能:

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

控制器

class HomeController < ApplicationController
  def index
    @user_id=3
  end   
end

型号:

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

2 个答案:

答案 0 :(得分:4)

家庭控制器:

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

用户模型:

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

现在,您不需要辅助方法,在视图中可以使用:

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

我会让你弄清楚其余部分,但我希望你能够将逻辑推入模型中。

答案 1 :(得分:0)

大多数逻辑属于User模型。没有其他任何东西需要实际进行这些计算,User模型可以访问所有相关的部分。还可以进行其他几项改进。我将尝试在下面添加评论以表明这些改进。

模型

class User < ActiveRecord::Base
  has_many :statuses
  has_and_belongs_to_many :friends,            # now you can just say user.friends
    :class_name => 'User',                     # makes more sense semantically    
    :join_table => "user_connections",
    :foreign_key => "user1_id",
    :association_foreign_key => "user2_id"

  def friends_statuses
    (friends.map(&:statuses).flatten + statuses).sort_by!(&:created_at).reverse
    # Ruby has many great methods for Arrays you should use.
    # You can often avoid instantiating variables like the empty Arrays you have.
  end

  def second_order_friends
    (friends.map(&:friends).flatten.uniq - friends) - [self]
  end
end

控制器

class HomeController < ApplicationController
  def index
    user = User.find(7) # how do you decide which user you're displaying things for?
                        # this might be better off in 'show' rather than 'index'

    # here you can call all the methods you have for 'User', such as:
    # user.friends, user.statuses, user.friends_statuses, user.second_order_friends

    # to make things accessible in the view, you just need an @variable, e.g.:
    @friends = user.friends
    @latest_statuses = user.friends_statuses.first(10)
  end