如何将消息发送给所有用户?

时间:2013-03-05 15:21:33

标签: ruby-on-rails ruby-on-rails-3

在这里,我用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 个答案:

答案 0 :(得分:1)

如果您使用的是mysql / sqlite3

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