2013-03-05 103 views
0

在这里,我通过params来定义接收者。
如果我想将消息发送给所有已经同时确认的用户,该怎么办?
我该怎么写?如何将消息发送给所有用户?

任何人有任何想法?

控制器

recipient = User.find_by_username(params[:messages][:recipient]) 

    if recipient.confirmed_at.nil? 
     redirect_to messages_sent_path 
     flash[:notice] = "This user hasn't confirmed yet!" 
     return 
    end 

    params[:messages][:subject] = 'no subject' if params[:messages][:subject].blank? 
    subject = params[:messages][:subject] 
    body = params[:messages][:body] 

    if current_user != recipient 
     current_user.send_message(recipient, body, subject) 
     redirect_to :controller => 'messages', :action => 'sent' 
     flash[:notice] = "Sent!" 
    else 
     redirect_to :controller => 'messages', :action => 'received' 
     flash[:notice] = "Cannot send to yourself!" 
    end 

回答

1

如果你使用MySQL/sqlite3的

users = User.where('confirmed_at IS NOT NULL') 
users.each do |user| 
    current_user.send_message(user, body, subject) 
end 
+0

谢谢:)请让我试试:) – HUSTEN 2013-03-05 15:24:50

+0

它不应该是'users.each做|收件人|'?? – HUSTEN 2013-03-05 15:31:37

+0

我的坏,改变了一点 – Intrepidd 2013-03-05 15:32:58

相关问题