# Find.collect problem

**URL:** <https://www.ruby-forum.com/t/find-collect-problem/97949>\
**Category:** Rails\
**Created:** [January 27, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/find-collect-problem/97949 "2006-01-27T16:19:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 27, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/find-collect-problem/97949/1 "2006-01-27T16:19:46Z")

</div>

Hi all.

I am trying to populate a dropdown based on two values.

def self.clientref( reference )  
@bugclientrefs = self.find\_all([“bug\_id = ?”, reference]).collect {|b|  
[b.client\_id.strip \<\< " - " \<\< b.client\_ref.strip]}  
end

This works fine. However this returns the reference to the client. I  
want the full name using this function.

class Bugclient \< ActiveRecord::Base  
set\_primary\_key “bc\_ref”

def self.fullname( reference )  
result = Bugclient.find(reference.strip)  
result.bc\_name  
end  
end

However I am not sure how to combine the two so that the client full  
name is collected instead of the reference.

I have tried the simpleton approach of

@bugclientrefs = self.find\_all([“bug\_id = ?”, reference]).collect {|b|  
[ Bugclient.fullname(b.client\_id.strip) \<\< " - " \<\< b.client\_ref.strip  
]}

But that just returns errors

Does anyone know how to combine the two?

Thanks

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 27, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/find-collect-problem/97949/2 "2006-01-27T17:12:03Z")

</div>

On Jan 27, 2006, at 8:19 AM, Jeff J. wrote:

> This works fine. However this returns the reference to the client. I
> 
> But that just returns errors
> 
> Does anyone know how to combine the two?

It looks like you’re using a class method when you actually want a  
regular instance method:

class Bugclient \< ActiveRecord::Base  
set\_primary\_key “bc\_ref”

def fullname  
bc\_name  
end  
end

In fact, you don’t even need to write that method if you just refer  
to bc\_name directly (assuming bc\_name is a column in the bugclients  
table):

def self.clientref( reference )  
@bugclientrefs = self.find\_all([“bug\_id = ?”, reference]).collect  
{|b| [b.bc\_name.strip \<\< " - " \<\< b.client\_ref.strip]}  
end

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 27, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/find-collect-problem/97949/3 "2006-01-27T17:28:04Z")

</div>

Duane J. wrote:

> On Jan 27, 2006, at 8:19 AM, Jeff J. wrote:
> 
> > This works fine. However this returns the reference to the client. I
> > 
> > But that just returns errors
> > 
> > Does anyone know how to combine the two?
> 
> It looks like you’re using a class method when you actually want a  
> regular instance method:
> 
> class Bugclient \< ActiveRecord::Base  
> set\_primary\_key “bc\_ref”
> 
> def fullname  
> bc\_name  
> end  
> end
> 
> In fact, you don’t even need to write that method if you just refer  
> to bc\_name directly (assuming bc\_name is a column in the bugclients  
> table):
> 
> def self.clientref( reference )  
> @bugclientrefs = self.find\_all([“bug\_id = ?”, reference]).collect  
> {|b| [b.bc\_name.strip \<\< " - " \<\< b.client\_ref.strip]}  
> end
> 
> Duane J.  
> (canadaduane)  
> [http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

Whoops, I am not following you 100% here (This is what I get for copoy /  
pasting / mangling a function without fully understanding it 🙂

I don’t think I gave you the full story either. def self.clientref(  
reference ) is in another class which doesn’t hold the full client name.  
I’ll give you the full source.

* * *

class Bugclientref \< ActiveRecord::Base  
set\_primary\_key “bug\_id”

def self.clientref( reference )  
@bugclientrefs = self.find\_all([“bug\_id = ?”, reference]).collect  
{|b| [b.bc\_name.strip \<\< " - " \<\< b.client\_ref.strip]}  
end  
end

* * *

class Bugclient \< ActiveRecord::Base  
set\_primary\_key “bc\_ref”

def self.fullname( reference )  
result = Bugclient.find(reference.strip)  
result.bc\_name

end  
end

* * *

So the collect SHOULD be trying to get the fullname from Bugclient based  
on the reference in Bugclientref and adding it to the Reference from  
Bugclientref.

Does this make sense? It hurts my head thinking about it. I want to kill  
the guy who came up with this schema.

Don’t you love newbies? 🙂

Thanks

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 27, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/find-collect-problem/97949/4 "2006-01-27T22:57:06Z")

</div>

On Jan 27, 2006, at 9:28 AM, Jeff J. wrote:

> > It looks like you’re using a class method when you actually want a  
> > end
> 
> reference ) is in another class which doesn’t hold the full client  
> {|b| [b.bc\_name.strip \<\< " - " \<\< b.client\_ref.strip]}  
> result.bc\_name
> 
> Does this make sense? It hurts my head thinking about it. I want to  
> kill  
> the guy who came up with this schema.
> 
> Don’t you love newbies? 🙂
> 
> Thanks
> 
> Jeff

What kind of errors are you getting? Is it possible that some of the  
"b.client\_id.strip"s that you’re passing in don’t exist in the  
Bugclient table? Maybe you’re doing it right, but the state of the  
database integrity just isn’t what you’re expecting.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 28, 2006, 2:05am UTC](https://www.ruby-forum.com/t/find-collect-problem/97949/5 "2006-01-28T02:05:14Z")

</div>

Duane J. wrote:

> On Jan 27, 2006, at 9:28 AM, Jeff J. wrote:
> 
> > > It looks like you’re using a class method when you actually want a  
> > > end
> > 
> > reference ) is in another class which doesn’t hold the full client  
> > {|b| [b.bc\_name.strip \<\< " - " \<\< b.client\_ref.strip]}  
> > result.bc\_name
> > 
> > Does this make sense? It hurts my head thinking about it. I want to  
> > kill  
> > the guy who came up with this schema.
> > 
> > Don’t you love newbies? 🙂
> > 
> > Thanks
> > 
> > Jeff
> 
> What kind of errors are you getting? Is it possible that some of the  
> "b.client\_id.strip"s that you’re passing in don’t exist in the  
> Bugclient table? Maybe you’re doing it right, but the state of the  
> database integrity just isn’t what you’re expecting.
> 
> Duane J.  
> (canadaduane)  
> [http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

Since I am not at work until Monday now I will have to get back to you.  
Thanks for all the help!

Jeff
