# Activerecord with JDBC in JRuby

**URL:** <https://www.ruby-forum.com/t/activerecord-with-jdbc-in-jruby/124245>\
**Category:** Rails\
**Created:** [December 27, 2007, 1:28am UTC](https://www.ruby-forum.com/t/activerecord-with-jdbc-in-jruby/124245 "2007-12-27T01:28:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [December 27, 2007, 1:28am UTC](https://www.ruby-forum.com/t/activerecord-with-jdbc-in-jruby/124245/1 "2007-12-27T01:28:55Z")

</div>

I have some code that works in “normal” RoR.

It doesn’t work in JRuby with the MySql JDBC adapter.

The query is:  
@breakdowns = Client.find(:all,  
:select =\> breakdown\_select,  
:conditions =\>breakdown\_conditions ,  
:joins =\> breakdown\_joins,  
:group =\> breakdown\_group,  
:order=\> ‘record\_count desc’)

Which shows up in the log as:

Client Load (0.000000) SELECT clients.client\_status as  
breakdown\_id,count(clients.client\_status)as record\_count FROM clients  
left join programmes on clients.programme\_id = programmes.id WHERE  
(clients.programme\_id = 12 ) GROUP BY clients.client\_status ORDER BY  
record\_count desc

This is fine…  
But when I try to access the “breakdown\_id” column from the query  
(clients.client\_status as breakdown\_id) it fails with:  
NoMethodError (undefined method `breakdown\_id’ for #\<Client:  
0xfd9b4d\>):

eg.  
for breakdown in @breakdowns  
logger.info("breakdown\_id= #{breakdown.breakdown\_id})  
end

This has been working fine until I tried to deploy with JRuby.

Any ideas… surely I can alias column names?

Cheers  
Giorgio.

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [January 17, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/activerecord-with-jdbc-in-jruby/124245/2 "2008-01-17T13:46:10Z")

</div>

Uhmm…

Can you try do?:

for breakdown in @breakdowns  
logger.info("breakdown\_id= #{breakdown.id})  
end

I have read that ActiveRecord always name de the primary key  
column with the name “id”.

I have this table definition in a jruby program:

class Empresa \< ActiveRecord::Base  
set\_table\_name “si\_Empresas”  
set\_primary\_key “IdEmpresa”  
has\_many :usuarios, :class\_name =\> “Usuario”, :foreign\_key =\>  
“IdEmpresa”  
has\_and\_belongs\_to\_many :perfiles,  
:class\_name =\> “Perfil”,  
:join\_table =\> “si\_EmpresasPerfiles”,  
:foreign\_key =\> “IdEmpresa”,  
:association\_foreign\_key =\> “IdPerfilEmpresa”  
end

and then i use this with success:

Empresa emp = Empresa.find(2234)  
…  
puts “Empresa #{emp.id}”

giorgio wrote:

> I have some code that works in “normal” RoR.
> 
> It doesn’t work in JRuby with the MySql JDBC adapter.
> 
> The query is:  
> @breakdowns = Client.find(:all,  
> :select =\> breakdown\_select,  
> :conditions =\>breakdown\_conditions ,  
> :joins =\> breakdown\_joins,  
> :group =\> breakdown\_group,  
> :order=\> ‘record\_count desc’)
> 
> Which shows up in the log as:
> 
> Client Load (0.000000) SELECT clients.client\_status as  
> breakdown\_id,count(clients.client\_status)as record\_count FROM clients  
> left join programmes on clients.programme\_id = programmes.id WHERE  
> (clients.programme\_id = 12 ) GROUP BY clients.client\_status ORDER BY  
> record\_count desc
> 
> This is fine…  
> But when I try to access the “breakdown\_id” column from the query  
> (clients.client\_status as breakdown\_id) it fails with:  
> NoMethodError (undefined method `breakdown\_id’ for #\<Client:  
> 0xfd9b4d\>):
> 
> eg.  
> for breakdown in @breakdowns  
> logger.info("breakdown\_id= #{breakdown.breakdown\_id})  
> end
> 
> This has been working fine until I tried to deploy with JRuby.
> 
> Any ideas… surely I can alias column names?
> 
> Cheers  
> Giorgio.
