Forum: Ruby on Rails find.collect problem.

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.
Jeff J. (Guest)
on 2006-01-27 17:19
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
Duane J. (Guest)
on 2006-01-27 18:12
(Received via mailing list)
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/
Jeff J. (Guest)
on 2006-01-27 18:28
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/

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
Duane J. (Guest)
on 2006-01-27 23:57
(Received via mailing list)
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/
Jeff J. (Guest)
on 2006-01-28 03:05
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/

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

Jeff
This topic is locked and can not be replied to.