Rails 4:通过模型定位项目

Rails 4: Target items through a model

我是 Rails 的新手,我创建了 FavoriteUser 模型以允许用户收藏其他用户。那很好用。我在 Tool(另一个模型)Index 视图中显示当前用户的收藏用户。

现在我也想在该页面上显示收藏用户创建的工具,但我不知道如何实现。

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

为喜欢的用户发布的新闻集工具添加循环!

%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)

在此先感谢您的帮助!

如果您希望获得 `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 最喜欢的 usersusers@tools,请尝试以下操作:

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