0

I am running this query and on puts I am getting an error TypeError: no implicit conversion of String into Integer if I just try to get the ID from the array.

puts billing_ids then the output is [<Billing id: 66, date: "2019-11-31", created_at: "2019-04-22 22:28:23", updated_at: "2020-01-15 17:03:05">]

And if I do puts "#{billing_ids["id"]}" to get just the id then I get the error TypeError: no implicit conversion of String into Integer

Please help me figure out how can I get the ID.

Office.all.each do |office|
  billing_ids=[] #initialize array
  office.issues.where("issues.amount > 0").each do |issue|
    billing_ids << issue.billings.where("billings.date < ?", Message.last.date).order(:date).last.id #add id to array
  end
  puts "#{billing_ids["id"]}"
end

Output

[#<Billing id: 66, date: "2019-11-31", created_at: "2019-04-22 22:28:23", updated_at: "2020-01-15 17:03:05">]
5
  • 1
    can you just do puts billing_ids and post the output here Commented Jan 16, 2020 at 5:10
  • @gautam output added Commented Jan 16, 2020 at 5:14
  • What do you want to accomplish ? print all Billing ids in a string ? Commented Jan 16, 2020 at 5:15
  • can you even use " insids " ? after " , I think one quotation must be used? look syntax error ? Commented Jan 16, 2020 at 5:16
  • @Navroop You should do billing_ids.map(&:id) as your billing_ids array has objects and not just ids Commented Jan 16, 2020 at 5:25

3 Answers 3

2

Looks like you are trying to print the ids of the objects which are in a Array.

Try this: puts "{billing_ids.map(&:id)}"

About the error TypeError: no implicit conversion of String into Integer

The error is because you are tring to access an index "id" of an Array of Billing objects. The id attribute of a particular object can be obtained but if you try accessing id of the Array, thats not going to work. (Note billing_ids is an Array as per your code). Array can be accessed by indices which are integers (unlike Hash)

Sign up to request clarification or add additional context in comments.

Comments

0

You are initializing billing_ids as an empty array and then pushing a billing model ID (an int) into it. If you do billing_ids.first you will then get the ID you're fetching in: issue.billings.where("billings.date < ?", Message.last.date).order(:date).last.id.

So try puts "#{billing_ids.first}". This should get your ID.

Note: the << operator appends—pushes the given object on to the end of the array, and since you are pushing an ID, not a billing model into billing_ids, you cannot access the model property ['ID'] of an ID.

Comments

0

you need to change billing_ids << issue.billings.where("billings.date < ?", Message.last.date).order(:date).last.id this line to billing_ids << issue.billings.where("billings.date < ?", Message.last.date).order(:date).last.billing.id OR billing_ids << issue.billings.where("billings.date < ?", Message.last.date).order(:date).last.pluck(:id)

Comments

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.