3

I am new to rails and have a task that asks me to send an invitation for any user to be admin in my magazine here is my piece of code

def invite
    inviteUser = { 'user_id' => current_user.id, 'Magazine_id' => params[:id] }
    CollaborationInvitation.create(inviteUser)
    @magazine = Magazine.find(params[:id])
    redirect_to :back
    rescue ActionController::RedirectBackError
    redirect_to root_path
end

I need to replace current_user.id with something that refers to any user's id which exists in my database to send him an invitation to be admin with me I tried to add @User=Users.All and then pass it as a variable but it got me an error I tried a lot of things but every time I get an error except for adding current_user.id

ps: I am using devise for authentication

LSDrako
  • 63
  • 1
  • 8
  • It should be `@users = User.all` which will return you all the users in table, try `@user = User.last`. Or you can also loop through all the users and send invite to all `User.all.each do |user| ..do your logic here wth the user.. end` – Sonalkumar sute May 02 '15 at 19:10
  • @Sontya I don't need to send to all users I just need to send to a one user by inserting his id – LSDrako May 02 '15 at 19:19
  • then do it, `User.last.id` will give you last user in database or `User.first.id` will give you first user in database – Sonalkumar sute May 02 '15 at 19:22

3 Answers3

4

You asked a couple things, and it is kind of confusing what you want to do.

Here is how you get all ids of records in a model.

Rails4: User.ids

Rails3: User.all.map(&:id)

Or (not sure if #pluck is in Rails 3 or not)

User.pluck(:id) 

If you want to get a random user (you mentioned "any user") you could do.

User.find(User.pluck(:id).sample)

Though I think what you really want to do is to pass the id or some other attribute of a user as a param to the action and send that user an invitation.

Presumably you either have a post or get route for "users#invite" (the action you wrote in your question). You can add a named parameter there or you can pass a url param or if you are using a post route, you could add the param to the post body.

Then in your contoller you can do something like this (I'll use email as an attribute):

def invite
  @user = User.find_by(email: params[:user_email])

  #Rails 3 like this
  # @user = User.find_by_email(params[:user_email])
   # now do stuff with user
end
Abdul Baig
  • 3,683
  • 3
  • 21
  • 48
  • I used User.find(User.pluck(:id).sample) with id 18 which exists in the database it didn't raise any error but It didn't send it to the user with the id 18 – LSDrako May 02 '15 at 20:29
  • :id is the symbol. If you already know the id you can just do User.find(18) –  May 02 '15 at 20:32
  • Here is the documentation for pluck http://apidock.com/rails/ActiveRecord/Calculations/pluck. Basically, it returns an array of the values of the attributes passed. Then Array#sample picks a random item in the array. So basically `User.find(User.pluck(:id).sample)` means find the first User where user.id is equal to a random id from a list of all user ids. –  May 02 '15 at 20:39
3

User.all will return you the collection of users. So, Find the user object to get an id... Try this code....

def invite
     inviteUser = { 'user_id' => User.find_by_email('user@example.com').id, 'Magazine_id' => params[:id] }
     CollaborationInvitation.create(inviteUser)
     @magazine = Magazine.find(params[:id])
     redirect_to :back
     rescue ActionController::RedirectBackError
     redirect_to root_path
 end
  • this sends to a single user I listed all users so I can send the invitation to any user I need just by clicking on invite link but this always sends to a single user and I need to make it user friendly ` <%= u.firstname%> <%=link_to'Invite'.html_safe , controller: :magazines, action: :invite, method: :put, u_id: u.id %> ` – LSDrako May 04 '15 at 22:41
1

You can try

User.last.id

or

User.find_by_email("xyz@test.com").id

or

User.where(email: "xyz@test.com").first.id

Replace xyz@test.com with desired user email address. To get more details on rails active record query interface, please read rails guides http://guides.rubyonrails.org/active_record_querying.html

Shahzad Tariq
  • 2,767
  • 1
  • 22
  • 31