Rails - 显示用户在上个月开始关注的用户 [英] Rails - Show the users that a user started following in the last month
问题描述
对这个问题表示歉意,我还在学习 Rails.我试图在我的 html 中显示 - 用户在上个月内开始关注的所有用户(即您最近关注的用户).我尝试了两种方法——都没有成功.我的第一次尝试是在我的控制器中,但我最接近的是向用户展示我已经开始关注上个月创建的用户.我的第二次尝试是在我的用户模型中 - 但我得到了未定义的方法 `call' # 你的意思是?来电者
following.html.erb
<div id="fire" class="tab-pane淡入淡出"><% @followingurecent.each 做 |recentfollowing|%><div class="box"><中心><%= image_tag 最近关注.avatar,宽度:85%></中心><%结束%>
Users_controller.rb
def 以下@user = User.find(params[:id])现在 = 时间.现在@followingurecent = @user.following.where(created_at: (now - 1.month)..Time.now)结尾
User.rb
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", 依赖: :destroyhas_many:passive_relationships,class_name:关系",foreign_key:followed_id",依赖::destroyhas_many :following, through: :active_relationships, source: :followedhas_many :followers, 通过::passive_relationships, 来源::follower定义跟随(其他)active_relationships.create(followed_id: other.id)Notification.create(收件人:@user,演员:User.current_user,动作:已关注",通知:@user)结尾def取消关注(其他)active_relationships.find_by(followed_id: other.id).destroy结尾def跟随?(其他)以下.包括?(其他)结尾def跟随最近现在 = 时间.现在self.following.where(active_relationships.where.(created_at: (now - 1.day)..Time.now))结尾
我会选择控制器解决方案.
但问题是我将寻找关系创建时间,而不是用户(它是用户,对,我的意思是 Follower 是用户模型,对吗?).
所以:
def 以下@user = User.find(params[:id])# 通过关系找到关注者 idfollowing_user_ids = @user.passive_relationships.where(created_at: (Time.now - 1.month)..Time.now).map(&:follower_id)# 然后获取关注者@followingurecent = User.where(id:following_user_ids)结尾
Apologies for the question, I'm still learning rails. I'm trying to show in my html - all the users that a user has started following within the last month (i.e. recent users you've followed). I've tried two approaches-both unsuccessful. My first try was in my controller but the closest I got was showing users I've started following who were created in the last month. My second try was in my user model - but I get undefined method `call' for # Did you mean? caller
following.html.erb
<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>
Users_controller.rb
def following
@user = User.find(params[:id])
now = Time.now
@followingurecent = @user.following.where(created_at: (now - 1.month)..Time.now)
end
User.rb
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
I'd go for the controller solution.
But the matter is that I will be looking for the relation creation time, not the user (it is user, right, I mean Follower is User model, right?).
So:
def following
@user = User.find(params[:id])
# find the follower ids through the relations
following_user_ids = @user.passive_relationships.where(created_at: (Time.now - 1.month)..Time.now).map(&:follower_id)
# then get the followers
@followingurecent = User.where(id: following_user_ids)
end
这篇关于Rails - 显示用户在上个月开始关注的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!