1

I have an issue calling objects within an array of ActiveRecord objects.

Here is my controller code building an array of things:

onething = Thing.where(this: id, that: id)
@things.push(onething) if onething.present?

This is looped in order to build an array of specific things with different ids passed inside the where method for this and that.

This and That being parents from thing:

class Thing < ApplicationRecord
  belongs_to :this
  belongs_to :that
end

Though in my view, when I call elements of the @things variable I get undefined methods errors.

When showing the @things variable in my view, in order to debug, I get things like this:

[#<ActiveRecord::Relation [#<Thing id: 1 ....>]>, #<ActiveRecord::Relation [#<Thing id: 2.......>]>]

Whereas, a variable with records coming from a direct query such as Thing.find(params[:id]) return something slightly different :

#<Thing id: 1, ....>

Why does the first one doesn't allow me to query the objects with simple queries such as Thing.id as the second works perfectly fine?

1 Answer 1

2

It's because you're pushing an ActiveRecord::Relation to the @things array (assuming that @things is indeed an array). So instead of ending up with an array of objects, you're winding up with an array consisting of arrays of objects. Use concat instead:

onething = Thing.where( this: id, that: id)
@things.concat(onething) if onething.present?

This will combine the two collections of objects into a single array.

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

7 Comments

Damn, spot on. Your solution does work. @things is indeed an array, i declared it as such before the loop: @things = [] This was the only way I found to create this stack of objects. When I tried to add the onething result directly into the variable I got an error and had to declare @things first. Sole solution I found was to make it an array...
Do you see a better solution than the one we eventually came to above ? Or is it just the correct way to sequentally create a stack of active record objects ?
On last question: why do I not get an Active Record object when I assign the result of Model.find(something) into a variable ? (I mean I just get the object, without the ActiveRecord::relation text before it)
find() is designed to return a single ActiveRecord object. where() is designed to return any matching records as an ActiveRecord::Relation. To see this in action, try these two different queries to see the difference in the SQL that's generated in the console – Thing.where(id: [1,2,3]) vs. Thing.find_by(id: [1,2,3]. They'll produce the exact same SQL, with the exception that find_by() includes LIMIT 1.
To answer your other question, I'd need to see the rest of your code to see what you're doing. I'm more than happy to help, but we should probably do that outside of SO so we don't muddy up the 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.