Rails 4:通过模型定位项目

时间:2016-09-08 16:24:48

标签: ruby-on-rails ruby ruby-on-rails-4 controller associations

我是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

为喜爱的用户发布的Newset工具添加循环!

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

提前感谢您的帮助!

1 个答案:

答案 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

如果您希望获得@toolsusers收藏的current_user users,请尝试以下操作:

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