# Expecting a definrd model id, but returning object id

**URL:** <https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258>\
**Category:** Rails\
**Created:** [January 18, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258 "2007-01-18T16:06:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/1 "2007-01-18T16:06:27Z")

</div>

@u=User.find(session[:user\_id])  
@friends=@u.friends  
#puts @friends  
#render :partial =\> “friend”, :collection =\> @friends

@friendpicks =@u.friends.collect{|x| x.products.sort\_by{|x|  
x.id}.reverse.first(10)}

--------------------------so far no  
problem---------------------------------------

@friendpicks.id  
(irb):21: warning: Object#id will be deprecated; use Object#object\_id  
=\> 29883400

* * *

I’ve checked that @friendpicks is an array with  
#Product:0x449263c  
#Product:0x4492678  
#Product:0x44926b4  
#Product:0x448cb88

but somehow they are all treated as objects and all the methods for  
product becomes strangers.

Anyone knows why I’m getting this?  
Any help is highly appreciated.

Abon

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/2 "2007-01-18T16:20:34Z")

</div>

The answer is that @friendpicks is an array and that you are getting  
the object id of the array, not the model ids of the contents of the  
@friendpicks array.

You probablly want something more like

@friendpicks.each do |pick|  
pick.id  
end

On 1/18/07, Bontina C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> --------------------------so far no  
> #Product:0x44926b4
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
-Dan Nugent

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 19, 2007, 2:52am UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/3 "2007-01-19T02:52:34Z")

</div>

Daniel N. wrote:

> The answer is that @friendpicks is an array and that you are getting  
> the object id of the array, not the model ids of the contents of the  
> @friendpicks array.
> 
> You probablly want something more like
> 
> @friendpicks.each do |pick|  
> pick.id  
> end
> 
> On 1/18/07, Bontina C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > --------------------------so far no  
> > #Product:0x44926b4
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> -Dan Nugent

It still returns the object id.  
Anyway I can turn it to a product?  
Thanks

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/4 "2007-01-19T16:31:57Z")

</div>

Hmm… sorry, I think I missed a level of nesting.

@friendpicks = @u.friends.collect do |friend|  
friend.products.sort\_by do |product|  
product.id  
end.reverse.first(10)  
end

So, if I’ve reinterpreted this right, you’re getting a list of lists  
of 10 products each, sorted by their id, then reversed.

So to access each of the lists you want @friendpicks.each. Then to  
access each of the picks lists you want @friendpicks.each{|pick|  
pick.each} Which will ultimately get you:

@friendpicks.each do |picks|  
picks.each do |pick|  
pick.id  
end  
end

Now, if you’ve got your console open, you can check the class of each  
of the objects by using the class method, and if you want to inspect  
them, try either p or inspect.

On 1/18/07, Bontina C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> >

–  
-Dan Nugent

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 20, 2007, 6:25am UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/5 "2007-01-20T06:25:53Z")

</div>

Okey doke. I thought you just wanted to iterate through the elements.  
In that case, just add .flatten to the end of that method chain, like  
so:

@friendpicks =@u.friends.collect{|x| x.products.sort\_by{|x|  
x.id}.reverse.first(10)}.flatten

Now, @friendpicks is an array of product objects. Sorry I  
misunderstood!

On 1/19/07, Bontina C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
-Dan Nugent

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 20, 2007, 5:27am UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/6 "2007-01-20T05:27:37Z")

</div>

I am not sure I’m getting [[p p][p p][p p p]]  
or [p p p p p p].  
I would like a [[p p][p p][p p p]] transforming to a [p p p p p p].  
Thanks

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 21, 2007, 3:34am UTC](https://www.ruby-forum.com/t/expecting-a-definrd-model-id-but-returning-object-id/82258/7 "2007-01-21T03:34:11Z")

</div>

Thanks for all your help.

I’m get an array of products now.
