0

私はかなり新しいRailsです。FavoriteUserモデルを作成して、ユーザーが他のユーザーを好きにしてもらえるようにしました。それはうまく動作します。 Tool(別のモデル)Indexビューに現在のユーザーのお気に入りのユーザーを表示しています。Rails 4:モデルを使用したアイテムのターゲット

また、お気に入りのユーザーが作成したツールをそのページに表示することもできますが、その方法はわかりません。

class User < ActiveRecord::Base 
    has_many :tools 
    has_many :favorite_users # just the 'relationships' 
    # Favorite users of user 
    has_many :favorite_relationships, class_name: "FavoriteUser", foreign_key: "c_user_id" 
    has_many :userfavorites, through: :favorite_relationships, source: :user 

    # Favorited by a user 
    has_many :favorited_relationships, class_name: "FavoriteUser", foreign_key: "user_id" 
    has_many :userfavorited_by, through: :favorited_relationships, source: :c_user 
end 

class Tool < ActiveRecord::Base 
    belongs_to :user 
end 

class FavoriteUser < ActiveRecord::Base 
    belongs_to :c_user, class_name: "User" 
    belongs_to :user, class_name: "User" 
end 

class ToolsController < ApplicationController 
    def index 
    @favorites = current_user.favorites.order("created_at DESC") 
    @userfavorites = current_user.userfavorites.order("created_at DESC") 
    # adding '@userfavoritestools' or similar 
    @tools = Tool.where(user_id: current_user).order("created_at DESC") 
    @user = current_user 
    end 
end 

# app/views/tools/index.html.haml  
%h2 My Favorite Tools 
- @favorites.each do |tool| 
    = image_tag tool.cover_filename.url 
    %h2= link_to tool.title, tool 
    %p= tool.subtitle 
    %p= tool.tag_list 
    %p= tool.impressionist_count 
    %p= link_to tool.get_upvotes.size, like_tool_path(tool), method: :get 
    %p= link_to "Favorite", favorite_tool_path(tool, type: "favorite"), method: :get 
    %p= link_to "Unfavorite", favorite_tool_path(tool, type: "unfavorite"), method: :get 
    %p= link_to "Edit", edit_tool_path(tool) 
    %p= link_to 'http://ocubit.com/tools/'+tool.id.to_s 
    %p= time_ago_in_words(tool.created_at) 

%h2 My Favorite Users 
- @userfavorites.each do |user| 
    = image_tag gravatar_for user if user.use_gravatar == true 
    = image_tag user.avatar_filename.url if user.use_gravatar == false 
    %h2= link_to user.username, user 
    %p= link_to "Favorite", userfavorite_user_path(user, type: "favorite"), method: :get 
    %p= link_to "Unfavorite", userfavorite_user_path(user, type: "unfavorite"), method: :get 
    %p= user.tag_list 

好きなユーザーが公開したNewset Toolsのループを追加する!

%h2 My Tools 
- @tools.each do |tool| 
    = image_tag tool.cover_filename.url 
    %h2= link_to tool.title, tool 
    %p= tool.subtitle 
    %p= tool.tag_list 
    %p= tool.impressionist_count 
    %p= link_to "Edit", edit_tool_path(tool) 
    %p= link_to 'http://ocubit.com/tools/'+tool.id.to_s 
    %p= time_ago_in_words(tool.created_at) 

ありがとうございました!

+0

すでにアソシエーションを使用する方法はわかっています...あなたの試行を表示できますか? –

答えて

1

あなたは、 `CURRENT_USERの@toolsを取得するには、次を試して探している場合:その後、

def index 
    @favorites = current_user.favorites.order("created_at DESC") 
    @userfavorites = current_user.userfavorites.order("created_at DESC") 
    @tools = current_user.tools.order("created_at DESC") 
    @user = current_user 
end 

あなたが代わりにcurrent_user年代のあるusers@toolsusersをお気に入りに追加取得するために探している場合次のことを試してください。

def index 
    @favorites = current_user.favorites.order("created_at DESC") 
    @userfavorites = current_user.userfavorites.order("created_at DESC") 
    @tools = Tool.where(user_id: @userfavorites.collect(&:id)).order("created_at DESC") 
    @user = current_user 
end 

あるいは、次のようにあなたは、その方法Userモデルとユーザーにこのロジックを移動することができ:

class User < ActiveRecord::Base 
    def tools_of_favorited_users 
    Tool.where(user_id: userfavorites.collect(&:id)) 
    end 
end 

def index 
    @favorites = current_user.favorites.order("created_at DESC") 
    @userfavorites = current_user.userfavorites.order("created_at DESC") 
    @tools = current_user.tools_of_favorited_users.order("created_at DESC") 
    @user = current_user 
end 
+0

あなたは私を誤解したかもしれません。ツールループが正常に動作しています。私はちょうどループ(コードを編集した@IndexViewので、どこに表示されているか)には、すべてのあなたのお気に入りのユーザーによって発行された最新のツールがすべて含まれています。 – Gugubaight

+0

答えを更新しました。 – Dharam

+0

うまく働いてくれてありがとう! – Gugubaight

関連する問題