Help with 'method_missing Error

Hey Everyone,

I’m using JRuby with SWT to work up a ‘contacts’ app. I can successfully
display contacts in a table when I pass TableItem an array of strings.
But
if I subclass Contact from ActiveRecord and then iterate through my
resultset, I get an error.

Here’s some of my code:

class Contact < ActiveRecord::Base
end

@items = Contact.find(:all)

… # SWT calls

@items.each_index do |i|
puts “#{@items[i]} #{i}”
item = Swt::Widgets::TableItem.new(table, Swt::SWT::NONE, i)
item.set_text(@items[i].to_java(:string))
end

And here’s the output:
C:\jruby-swt-cookbook\samples>jruby grid.rb
loading swt/swt_win32
From items: record:1 Fred Apple [email protected]
From items: record:2 Joe Napolitano [email protected]
From items: record:3 Alice Laring [email protected]
#Contact:0x487c5f 0
c:/jruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in
method_missing': undef ined method to_java’ for #Contact:0x487c5f (NoMethodError)
from grid.rb:50:in initialize' from grid.rb:47:in each_index’
from grid.rb:47:in initialize' from grid.rb:93:in new’
from grid.rb:93

It seems that JRuby doesn’t know about Contacts…but I’m not
understanding
how to inform it.
Do I need to define an each method within my Contacts class? or perhaps
include an @items instance variable? or does to_java need to be told
about
Contacts and if so, how?

Help greatly appreciated!
Haden


View this message in context:
http://old.nabble.com/Help-with-'method_missing-Error-tp27677288p27677288.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

JRuby is perfectly able to convert a ruby string to a java string for
you, in almost all circumstances. That means that although
TableItem#set_text takes a Java String, you can treat it as though it
takes a Ruby string and it will all be handled for you.

In other words, try this:

item.set_text(@items[i].to_s)

best,
Dan

PS. .to_java is defined on only a limited set of Ruby types: arrays,
numbers, strings, that kind of thing. .to_java(:string) only really
makes sense when called on an array, and it means: convert this Ruby
array to a Java array, and make it a String[] rather than an Object[].


Daniel L.

twitter.com/danlucraft
danlucraft.com/blog

On 21 Feb 2010, at 22:14, haden wrote:

class Contact < ActiveRecord::Base
end
ined method `to_java’ for #Contact:0x487c5f (NoMethodError)
Contacts and if so, how?
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

PS. .to_java is defined on only a limited set of Ruby types: arrays,
numbers, strings, that kind of thing. .to_java(:string) only really
makes
sense when called on an array, and it means: convert this Ruby array to
a
Java array, and make it a String[] rather than an Object[].

Hi Dan,

Yes, this to_java(:string) works for an array of string arrays and is
very
convenient: just give it an index and it fills in the cells with each
item
in the sub-array. But, once i derive @items from ActiveRecord, then (if
I’m
understanding correctly) I’m passing it an Object with string fields. Is
there a way to pass in an object and have it apply the objects fields to
the
row? (see below for what worked for me)

Using item.set_text(@items[i].to_s) displays the Contact instance ID in
the
first (first_name) column - but nothing else.

So, I tried this -
item.set_text(@items[i].first_name)
item.set_text(@items[i].last_name)
item.set_text(@items[i].email)
which displays the (correct) email strings but also in first column.

Finally, I tried this -
item.set_text(0, @items[i].first_name)
item.set_text(1, @items[i].first_name)
item.set_text(2, @items[i].first_name)

which I don’t see in the API but which I saw in a JRuby example and ‘for
some reason’ works. Any idea?
Thanks!
Haden

View this message in context:
http://old.nabble.com/Help-with-'method_missing-Error-tp27677288p27682008.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I think you must have overlooked it because it is in the API docs. Thanks
to Eclipse’ awful new doc >>browser I don’t think I can link directly to
it,
but look in the TableItem API docs. 3rd from the bottom :slight_smile:

Geez - I wish could say I just glanced at the API but, I just didn’t see
it.
Oh well. Life may be offering up a metaphor.

Thanks for your suggestions and corrections!
Haden

View this message in context:
http://old.nabble.com/Help-with-'method_missing-Error-tp27677288p27692336.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On 22 Feb 2010, at 02:13, haden wrote:

Hi Dan,

Yes, this to_java(:string) works for an array of string arrays and is very
convenient: just give it an index and it fills in the cells with each item
in the sub-array. But, once i derive @items from ActiveRecord, then (if I’m
understanding correctly) I’m passing it an Object with string fields.

No, you were passing in @items[i] which is a Contact object.

Is there a way to pass in an object and have it apply the objects fields to the
row? (see below for what worked for me)
Using item.set_text(@items[i].to_s) displays the Contact instance ID in the
first (first_name) column - but nothing else.

There are many different ways to turn a Contact object into a string,
you have to pick one yourself depending on what you want displayed. If
to_s returns the id, then use something else.

So, I tried this -
item.set_text(@items[i].first_name)
item.set_text(@items[i].last_name)
item.set_text(@items[i].email)
which displays the (correct) email strings but also in first column.

Yep.

Finally, I tried this -
item.set_text(0, @items[i].first_name)
item.set_text(1, @items[i].first_name)
item.set_text(2, @items[i].first_name)

which I don’t see in the API but which I saw in a JRuby example and ‘for
some reason’ works. Any idea?

I think you must have overlooked it because it is in the API docs.
Thanks to Eclipse’ awful new doc browser I don’t think I can link
directly to it, but look in the TableItem API docs. 3rd from the bottom
:slight_smile:

best,

Dan


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email