0

I have a list of names and I need to get their unique ID's with ruby shell.

Example:

User.find_by(name: "James").id

Output:

=>  265

So I need to do something like:

User.find_by(name: "James").id && User.find_by(name: "Robert").id && User.find_by(name: "William").id

In order to get this output:

=> 265
=> 726
=> 24

What's the && for Ruby shell commands??

3
  • You can separate multiple commands using the semicolon ; Commented Jul 20, 2020 at 20:52
  • User.where(name: ['James'. 'Robert', 'William']).ids . ids Commented Jul 20, 2020 at 23:07
  • Your question is unclear. There is no such thing as "Ruby shell". What are you talking about? Are you talking about the "Ruby command prompt" that some installers for Windows create? That's not a "Ruby shell", that's just a normal Windows command prompt where the %PATH% environment variable has been modified to include the installation directory of Ruby. Commented Jul 21, 2020 at 4:00

3 Answers 3

1

&& is a logical/boolean "AND". The fact that these work in your example is due to how Ruby deals with truthy/falsy values – you can read the details here: https://www.rubyguides.com/2019/02/ruby-booleans/.

To hopefully answer your questions: I would go with an SQL inclusion operation somewhat like this:

relation = User.where(name: ["James", "Robert", "William"])

# If you really just are looking for the IDs, you can use this the following.
# Note that the order of the IDs isn't the same as the input parameters,
# so if you receive back 1, 5, 10, that doesn't mean that James = 1,
# Robert = 5 and William = 10. If you need a certain order, you need to
# add an `order` clause.

puts relation.ids

# If you want to do more with the result than just have its IDs, you can
# iterate over it like so:
relation.each do |user|
  puts "#{user.name}: #{user.id}"
end
Sign up to request clarification or add additional context in comments.

1 Comment

Minor comment you are missing a right bracket (]) also since the OP is looking for the ids there is literally a method called ids
0

If you really want that result:

=> 265
=> 726
=> 24

You can try something like that:

> ids = User.where(name:['James','Robert', 'William']).order(:name).ids 
> puts ids
265
726
24

1 Comment

This is exactly what I needed! Simple and straightforward, worked like a charm, thanks!
0

find_by will always return a single result if present.

You need to use where. Try the below code:

User.where("name like ? or name like ? or name like ?","%James%", "%Robert%", "%William").pluck(:id)

5 Comments

This will lead to an error because the way you have it, James, Robert and William are directly interpolated and therefore presumed to be constants (which don't exist). Moreover, it's strongly discouraged to directly interpolate values in SQL because it can lead to SQL injection vulnerabilities.
Thank you, but some usernames have dots so this is the output: Traceback (most recent call last): 1: from (irb):1 NoMethodError (undefined method `Smith' for nil:NilClass)
@ClemensKofler Thanks, I mis-typed that, my bad, updated the answer, Fran What do you mean by dots..?
@ClemensKofler SQL Injection is not possible when using the above because the first element is a template and the latter are parameters to that template.
@ClemensKofler how should work on ruby's command shell? Do I need to compile .rb program first? {ruby newbie here}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.