A JRuby Sequence Engine
Because you want to let the EJB use Ruby code for the business logic, it makes sense to create
the Ruby code first and test it in isolation, making sure it works correctly before trying to
access it through a Java application server. You??™ll contain the Ruby engine code in one file,
and it will use ActiveRecord, but not the rest of Rails.
So, begin by requiring the necessary code:
require 'rubygems'
require 'active_record'
require 'active_record/connection_adapters/jdbc_adapter'
After you??™ve done that, you can establish a connection to the database, like this:
ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'com.mysql.jdbc.Driver',
:url => 'jdbc:mysql://localhost/seq',
:username => 'seq',
:password => 'Sekventially'
)
CHAPTER 9 ?– A JRUBY ENTERPRISE BEAN 170
Then you can create ActiveRecord classes for your model:
class AuthorizedUser < ActiveRecord::Base
has_many :authorizations, :foreign_key => 'authorized_user'
end
class Authorization < ActiveRecord::Base
belongs_to :authorized_user, :foreign_key => 'authorized_user'
end
class Sequence < ActiveRecord::Base
belongs_to :last_updated_by, :class_name => "AuthorizedUser",
:foreign_key => 'last_updated_by'
end
As you can see, you need to specify foreign keys for all associations, because you didn??™t
name them according to the Rails conventions. At the moment this code is bare, but you can
still do most things with it. However, you??™ll add several helper methods to the Sequence class
later.
Pages:
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264