Ruby on rails Rails-显示用户上个月开始跟踪的用户
抱歉,我还在学习rails。我试图在我的html中显示用户在上个月内开始跟踪的所有用户(即您最近跟踪的用户)。我试过两种方法都不成功我的第一次尝试是在我的控制器中,但最近的一次尝试是显示我开始跟踪上个月创建的用户我的第二次尝试是在我的用户模型中,但我得到了未定义的方法“call”,你是说?来电者 following.html.erbRuby on rails Rails-显示用户上个月开始跟踪的用户,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,抱歉,我还在学习rails。我试图在我的html中显示用户在上个月内开始跟踪的所有用户(即您最近跟踪的用户)。我试过两种方法都不成功我的第一次尝试是在我的控制器中,但最近的一次尝试是显示我开始跟踪上个月创建的用户我的第二次尝试是在我的用户模型中,但我得到了未定义的方法“call”,你是说?来电者 following.html.erb <div class="tab-content"> <div id="fire" class="tab-pane fade">
<div class="tab-content">
<div id="fire" class="tab-pane fade">
<% @followingurecent.each do |recentfollowing| %>
<div class="box">
<center>
<%= image_tag recentfollowing.avatar, width: 85 %>
</center>
</div>
<% end %>
</div>
</div>
def following
@user = User.find(params[:id])
now = Time.now
@followingurecent = @user.following.where(created_at: (now - 1.month)..Time.now)
end
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
def followingrecent
now = Time.now
self.following.where(active_relationships.where.(created_at: (now - 1.day)..Time.now))
end
User.rb
<div class="tab-content">
<div id="fire" class="tab-pane fade">
<% @followingurecent.each do |recentfollowing| %>
<div class="box">
<center>
<%= image_tag recentfollowing.avatar, width: 85 %>
</center>
</div>
<% end %>
</div>
</div>
def following
@user = User.find(params[:id])
now = Time.now
@followingurecent = @user.following.where(created_at: (now - 1.month)..Time.now)
end
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
def followingrecent
now = Time.now
self.following.where(active_relationships.where.(created_at: (now - 1.day)..Time.now))
end
我会选择控制器解决方案 但问题是,我将寻找的是关系创建时间,而不是用户(它是用户,对吧,我的意思是追随者是用户模型,对吧?) 因此:
非常感谢Ruby Racer-这非常有帮助,工作非常完美:)您还可以将时间范围重写为
1.month.ago..time.now