Add multiple user support for a function

16 views
Skip to first unread message

David Williams

unread,
Jan 14, 2015, 11:13:08 PM1/14/15
to rubyonra...@googlegroups.com
These two functions below work perfectly for end to end normal user
interaction. But, I need help giving line 2 of the code "recipients =
User.where(id: params['recipients']) multiple user support. As in allow
User model to interact with BizUser model.

def create
recipients = User.where(id: params['recipients'])
conversation = current_user.send_message(recipients,
params[:message][:body], params[:message][:subject]).conversation
flash[:success] = "Message has been sent!"
redirect_to conversation_path(conversation)
end

def recipients_options
s = ''
User.all.each do |user|
s << "<option value='#{user.id}'
data-img-src='#{gravatar_image_url(user.email, size:
50)}'>#{user.name}</option>"
end
s.html_safe
end

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jan 15, 2015, 4:08:07 AM1/15/15
to rubyonra...@googlegroups.com
On 15 January 2015 at 04:12, David Williams <li...@ruby-forum.com> wrote:
> These two functions below work perfectly for end to end normal user
> interaction. But, I need help giving line 2 of the code "recipients =
> User.where(id: params['recipients']) multiple user support. As in allow
> User model to interact with BizUser model.

What is BizUser?

Colin

>
> def create
> recipients = User.where(id: params['recipients'])
> conversation = current_user.send_message(recipients,
> params[:message][:body], params[:message][:subject]).conversation
> flash[:success] = "Message has been sent!"
> redirect_to conversation_path(conversation)
> end
>
> def recipients_options
> s = ''
> User.all.each do |user|
> s << "<option value='#{user.id}'
> data-img-src='#{gravatar_image_url(user.email, size:
> 50)}'>#{user.name}</option>"
> end
> s.html_safe
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/124796b85709e25d2c1b111fa8104b16%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

David Williams

unread,
Jan 15, 2015, 12:13:51 PM1/15/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1166693:
> On 15 January 2015 at 04:12, David Williams <li...@ruby-forum.com>
> wrote:
>> These two functions below work perfectly for end to end normal user
>> interaction. But, I need help giving line 2 of the code "recipients =
>> User.where(id: params['recipients']) multiple user support. As in allow
>> User model to interact with BizUser model.
>
> What is BizUser?
>
> Colin

I got it.

def create
recipients = User.where(id: params['recipients']) ||
BizUser.where(id: params['recipients'])
conversation = current_user.send_message(recipients,
params[:message][:body], params[:message][:subject]).conversation
flash[:success] = "Message has been sent!"
redirect_to conversations_index_path(conversation)
end

def recipients_options
s = ''
users = User.all + BizUser.all; users.each do |user|
s << "<option value='#{user.id}'
data-img-src='#{attachment_url(@user, user.email, :profile_avatar,
:fill, 30, 30)}'>#{user.username}</option>"

Daniel Loureiro

unread,
Jan 19, 2015, 8:29:48 PM1/19/15
to rubyonra...@googlegroups.com
Hi David,

but what if you have a User with id #10 and a BizUser with id #10? I don't know much more about your app, but why don't you send an additional param like params["user_type"], that will provide an "user" or a "biz"? Or why don't you use a global unique identificator instead of rely on the "id" field? Or why don't you add an "U" or "B" before the id on your view (on the form, the "recipients" html field) - so at "create" you can dismember the id from the user type and use the correct model?

I got it.

def create
    recipients = User.where(id: params['recipients']) ||
BizUser.where(id: params['recipients'])
    conversation = current_user.send_message(recipients,
params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversations_index_path(conversation)
  end  

Reply all
Reply to author
Forward
0 new messages