Ruby on rails 如何在Rails中添加类似twitter的@回复?
我完成了Hartl的教程,在过去的几天里一直在尝试添加,但都没有效果。我在MicroPost表中添加了一个in_reply_to列,作为一个整数,我认为可以用来引用给定用户的id。现在,我正在使用正则表达式通过MicroPost控制器搜索与给定用户名的匹配项 Hartl建议在Micropost模型中使用包含范围。我承认,基于rails创建的自动关联或我必须告诉它的内容,我不太确定在这个范围中包括什么 如蒙协助,将不胜感激 用户模型Ruby on rails 如何在Rails中添加类似twitter的@回复?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我完成了Hartl的教程,在过去的几天里一直在尝试添加,但都没有效果。我在MicroPost表中添加了一个in_reply_to列,作为一个整数,我认为可以用来引用给定用户的id。现在,我正在使用正则表达式通过MicroPost控制器搜索与给定用户名的匹配项 Hartl建议在Micropost模型中使用包含范围。我承认,基于rails创建的自动关联或我必须告诉它的内容,我不太确定在这个范围中包括什么 如蒙协助,将不胜感激 用户模型 has_many :microposts, dependen
has_many :microposts, dependent: :destroy
has_many :replies, through: :microposts, source: :in_reply_to
VALID_NAME_REGEX = /\A[\w+\-.]\z/i
validates :name, presence: true,
format: { with: VALID_NAME_REGEX },
length: { maximum: 20 },
uniqueness: { case_sensitive: false }
...
def feed
Micropost.from_users_followed_by(self)
Micropost.including_replies
end
微观模型
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
belongs_to :in_reply_to, class_name: "User"
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 }
default_scope order: 'microposts.created_at DESC'
scope :including_replies, where("user_id = in_reply_to")
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
end
Microposts控制器
class MicropostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
before_filter :reply_to_user, only: :create
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
redirect_to root_path
end
private
def correct_user
@micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
def reply_to_user
if reply_to = @micropost.content.match(/\A(@[\w+\-.])\z/i)
@other_user = User.where(name: reply_to.to_s[1..-1])
if @other_user && current_user.followed_users.includes(@other_user)
@micropost.in_reply_to = @other_user.id
end
end
end
end
我已经开始研究这一点,我注意到的第一件事是,您的regexp查找回复时不起作用:
> /\A(@[\w+\-.])\z/i.match("@someName blabla")
=> nil
因此,第一个技巧是:测试你正在实现的每一个细节。
还有,我想你把联想搞错了,我用了
has_many :replies, foreign_key: "to_id", class_name: "Micropost"
我将密钥从in_reply_重命名为,因为在使用下划线时出现了一个奇怪的弃用警告
但你主要是问如何在Micropost中定义新的范围。我是这样做的:
scope :from_users_followed_by_including_replies, lambda { |user| followed_by_including_replies(user) }
我希望这有帮助。您可以查看我的整个实现
奇怪的我相信您忘了在-。在[]中。现在能用了吗?谢谢。哦,对不起,我在这里抄的时候把它弄丢了。太好了!非常感谢。下次我会坚持为外键添加_id的惯例哈哈
def self.followed_by_including_replies(user)
followed_ids = %(SELECT followed_id FROM relationships
WHERE follower_id = :user_id)
where("user_id IN (#{followed_ids}) OR user_id = :user_id OR to_id = :user_id",
{ :user_id => user })
end