Reputation: 292
I have no idea what is going on with a query I am doing in Rails console. I am running Rails 4.1.5. First I get a group of items:
pages = Item.item_steps.map {|item| item.pages }
Once I have these pages, I then find out the class:
pages.class
#array
So, I now have an array. Easy enough as I know a ton of methods that you can run on an array. However, when I do any valid array method on the pages array it doesn't work. For example:
pages.map(&:id)
NoMethodError: undefined method `domain_id' for #<Page::ActiveRecord_Associations_CollectionProxy:0x007f4723af2208>
or
irb(main):090:0> pages.pluck("id")
NoMethodError: undefined method `pluck' for #<Array:0x007f4725681640>
I just want to get the "id" from the pages array, but nothing I know of works with this.
thanks Mike
Upvotes: 1
Views: 1887
Reputation: 3053
If you read carefully through the errors, I think you would see what the issue is. Your current query produces the following type of result:
[ACollectionOfPages, ACollectionOfPages, ...]
So your map
code is trying to call #id
on an ActiveRecord
collection, because that is what each element of your array is.
If you really want to use your current code, I'd run:
pages.map { |collection| collection.map(&:id) } # possibly with .flatten
You would be better of creating an association like:
class Item < ActiveRecord::Base
has_many :item_steps
has_many :pages, through: :item_steps
end
Then, Item.pages
will return an ActiveRecord
collection, so you could run Item.pages.pluck(:id)
.
Upvotes: 1