Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-显示用户在上个月发布过一本书的情况_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails Rails-显示用户在上个月发布过一本书的情况

Ruby on rails Rails-显示用户在上个月发布过一本书的情况,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,抱歉,我还在学习rails。我试图在我的html中显示用户在上个月内发布过一本书的所有用户(即您跟踪的最近发布过一本书的用户)。我试着遵循从这个问题中学到的经验教训,但我无法让它工作,也无法为#得到一个错误未定义的方法“books”。非常感谢你的帮助 following.html.erb <div id="wall" class="tab-pane fade"> <% @newpostuser.each do |newpost| %>

抱歉,我还在学习rails。我试图在我的html中显示用户在上个月内发布过一本书的所有用户(即您跟踪的最近发布过一本书的用户)。我试着遵循从这个问题中学到的经验教训,但我无法让它工作,也无法为#得到一个错误未定义的方法“books”。非常感谢你的帮助

following.html.erb

<div id="wall" class="tab-pane fade">
        <% @newpostuser.each do |newpost| %>
            <div class="box">
                <center>
                <%= image_tag newpost.avatar, width: 85 %>
                </center>
            </div>
        <% end %>
</div>
def following
 @user = User.find(params[:id])
 following_ids = @user.active_relationships.map(&:followed_id)
 @userfollowing = User.where(id: following_ids)
 newbook_user_ids = @userfollowing.books.where(created_at: (Time.now - 3.month)..Time.now)
 @newpostuser = User.where(id: newbook_user_ids)
end
has_many :books, dependent: :destroy
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower

def follow(other)
    active_relationships.create(followed_id: other.id)
    Notification.create(recipient: @user, actor: User.current_user, action: "Followed", notifiable: @user)
end
def unfollow(other)
    active_relationships.find_by(followed_id: other.id).destroy
end
def following?(other)
    following.include?(other)
end
User.rb

<div id="wall" class="tab-pane fade">
        <% @newpostuser.each do |newpost| %>
            <div class="box">
                <center>
                <%= image_tag newpost.avatar, width: 85 %>
                </center>
            </div>
        <% end %>
</div>
def following
 @user = User.find(params[:id])
 following_ids = @user.active_relationships.map(&:followed_id)
 @userfollowing = User.where(id: following_ids)
 newbook_user_ids = @userfollowing.books.where(created_at: (Time.now - 3.month)..Time.now)
 @newpostuser = User.where(id: newbook_user_ids)
end
has_many :books, dependent: :destroy
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower

def follow(other)
    active_relationships.create(followed_id: other.id)
    Notification.create(recipient: @user, actor: User.current_user, action: "Followed", notifiable: @user)
end
def unfollow(other)
    active_relationships.find_by(followed_id: other.id).destroy
end
def following?(other)
    following.include?(other)
end

首先,有很多东西可以简化。这些线路:

following_ids = @user.active_relationships.map(&:followed_id)
@userfollowing = User.where(id: following_ids)
可以写成:

@userfollowing = @user.followed
下一个问题是
books
方法应用于单个用户(它为一个用户返回图书),但您正在尝试将其应用于用户列表。如果它真的起作用,它将返回一个不是用户的书籍列表。在你的情况下,你应该能够写:

@newpostusers = @userfollowing.joins(:books).where(books: { created_at: (Time.now - 3.month)..Time.now) } )

一般来说,您希望尽量避免使用
id
s

,以防其他人遇到类似问题。Marc的解决方案在一秒钟内效果很好,但后来停止了工作,给了我下面列出的一个错误

[3,4,5]的未定义方法联接(undefined method joins):数组,你是指什么?加入 经过两个小时的尝试,我找到了一个解决方案——也许有一个更有效的方法,但这对我来说非常有效

def following
 @user = User.find(params[:id])
 userfollowing = @user.active_relationships.map(&:followed_id)          
 recentbook = Book.where(created_at: (Time.now - 1.month)..Time.now)
 uidsrecentbook = recentbook.map(&:user_id)
 common = (userfollowing & uidsrecentbook)
 @newpostuser = User.where(id: common)
end
要解释这里发生了什么:

首先,我收集最新创作的书籍

第二,我正在使用map搜索,以收集这些新书的用户ID

第三,我正在使用“&”的力量将我正在关注的用户ID与最新书籍的用户ID进行比较


第四,我与我的用户共同创建@newpositionr。这非常有效:)

Marc非常感谢这非常有帮助,你的解释真的帮助我更好地理解了这个问题。干杯,伙计:)