3

我对 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
4

2 回答 2

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

我会让你弄清楚其余的,但我希望你能大致了解将逻辑推入模型。

于 2012-10-13T14:31:26.020 回答
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
于 2012-10-13T15:13:16.373 回答