[问题] 类似fb讯息问题请教

楼主: icps (虹月)   2014-11-11 16:27:56
新手想做做一个类似FB讯息的东西
所以msg里面有 user_id (寄件人) touser(收件人) content(讯息内容)
unread(判断是否已读) newmessage(收到新讯息的数量)
我想问的问题是
一.有寄件人和收件人都使用user这个数据库 要怎么做关联?
class Msg < ActiveRecord::Base
belongs_to :owner, :class_name => "User", :foreign_key => :user_id
end
>>我想到的方法是 current_user.msgs.new 来关联user和msgs
但是收件人的部分 我是根据view获得的名称来寻找id
来判断要寄给谁并且判断是否为使用者 下面*注一
def validate_user
User.find_by_name(params.require(:msg).permit(:touser)[:touser])
end
二.已知道收件人 如何做回复讯息
>>我想到的方法有 1.把变量user传到另外一个页面 但是这部分失败..一直收不到
这部分想问要如何做
2.也是我目前的作法 #给回复用 *注二
做在同一页面 然后再view的show的页面直接提取收件人id
以上功能是都ok 但是想问做法会不会太奇爬Q_Q,或是效率太差
先感谢大大指教
# msgs_controller
def index
@msgs=Msg.where(touser: current_user.name).order("updated_at DESC")
#找收件人是自己的msgs
current_user.update_attribute("newmessage", "0") #进到讯息箱就清掉提示
end
def show
begin
current_user.update_attribute("newmessage", "0") #进到讯息箱就清掉提示
@msg = Msg.find(params[:id])
unless list_user_message?(@msg) #除非是收件或是寄件人
redirect_to msgs_path
end
@msg.update_attribute("unread", "false" ) #变为已读
@newmsg = current_user.msgs.new #给回复用 *注二
rescue
redirect_to msgs_path
end
end
def new
@msg = current_user.msgs.new
end
def create
@msg = current_user.msgs.build(msg_params)
if validate_user == nil #如果收件人不是使用者 *注一
render :new
elsif @msg.save
received=user_newmsg.newmessage #提取新讯息字段的数量
received+=1 #增加一
user_newmsg.update_attribute("newmessage", received) #更新提示新讯息数量
redirect_to msgs_path
end
end

Links booklink

Contact Us: admin [ a t ] ucptt.com