Forum: Ruby on Rails Strange id value in ActiveRecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
matthieu.riou (Guest)
on 2005-11-12 13:49
(Received via mailing list)
Hi,

I'm experimenting a strange behaviour when trying to evalute the id of
an
ActiveRecord instance. The code below produces weird values:

user = Customer.find(@session[:customer].id)
products = user.orders.collect do |order|
order.orderItems.collect do |item|
logger.info('Product : ' + item.product.id.to_s)
item.product
end.uniq
end
products.each { |p| logger.info <http://logger.info> 'Products ' +
p.id.to_s}

The log inside the 'collect' produces the expected result (ids like 3, 4
or). But when I iterate on the resulting Array (products), my id values
are
something like:

Products -608103678
Products -608105008
Products -608106288
Products -608112128
Products -608115738

Do anybody have an idea of what is happening?

Thanks a lot for any help,

Matt.
mrj (Guest)
on 2005-11-12 13:49
(Received via mailing list)
Matthieu R. wrote:

>     user = Customer.find(@session[:customer].id)
>     products = user.orders.collect do |order|
>       order.orderItems.collect do |item|
>         logger.info('Product : ' + item.product.id.to_s)
>         item.product
>       end.uniq
>     end
>     products.each { |p| logger.info <http://logger.info> 'Products ' +
> p.id.to_s }

products will be an array of arrays.  Instead of using collect in
the outer loop, you want to merge the inner arrays together, then
call uniq.  At the moment you're printing the Ruby id values of
the arrays that make up the products array.
This topic is locked and can not be replied to.