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 に答える