Legacy database and finder_sql nightmare!

This is my first rails app with a legacy database and I’m having a
terrible time getting the models set up correctly. I have an order
table that has a primary field named order_number. I have a name table
with a primary of item_number. These two tables are liked by the
item_number and the order_number, but not as you might think. If the
order_number is 2500, then each entry in the name table will have it’s
item number incremented by 1 (i.e. 2501,2501,2503). Here are my models

class Order < ActiveRecord::Base
set_table_name :order
set_primary_key :order_number
has_many :name, :finder_sql => ‘SELECT * from name
where name.item_number like
CONCAT(SUBSTRING(#{order_number},1,14),’%’)’

end
~
class Name < ActiveRecord::Base
set_table_name :name
set_primary_key :item_number
belongs_to :order, :finder_sql => ‘SELECT * from order
where order.order_number =
CONCAT(SUBSTRING(app_name.#{item_number},1,14),‘00’)’
end

These produce the following errors.

ArgumentError (Unknown key(s): finder_sql):
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/core_ext/hash/keys.rb:48:in
assert_valid_keys' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:519:inbelongs_to_without_reflection’
(eval):5:in belongs_to' /app/models/name.rb:4 /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:355:inhas_many_without_reflection’
(eval):5:in `has_many’
/app/models/order.rb:4

Can anyone help me out with this?

Hi, charlie,

as a first step please change set_primary_key :order_number to
set_primary_key “order_number” (and the like for :item_number) it might
well be that assert_valid_keys is stumbling on these.

Best Regards
Jan P.

Hi, charlie,

why is it has_many :name (and not names) is pluralization turned off?

Best regards
Jan

Jan P. wrote:

Hi, charlie,

as a first step please change set_primary_key :order_number to
set_primary_key “order_number” (and the like for :item_number) it might
well be that assert_valid_keys is stumbling on these.

Best Regards
Jan P.

I changed this but it didn’t help.

why is it has_many :name (and not names) is pluralization turned off?

I have it singular because the table name is actually singular. That’s
why I set the parameter (set_table_name :order). This is the correct
method, correct?

Hi, charlie,

another thing.

:finder_sql isn’t a supported option for belongs_to:

def belongs_to(association_id, options = {})
options.assert_valid_keys(:class_name, :foreign_key, :remote,
:conditions, :order, :include, :dependent, :counter_cache, :extend)

Beyond that :order is kind of a reserved word at least on relationships.
Don’t know if this causes problems as well, but the first one you need
to change is finder_sql.

Regards
Jan

try to


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Why are you providing the :conditions as an array? It should be a string
and you need to escape single quotes or use a mixture of single and
double quotes.

Is there anything happening in your development.log? The first thing you
need is a sql statement in your development.log, once you’ve got that
you could think about workarounds fixing that sql statement.

Jan

Order is just a made up name, the actual table name is different.

Here is my new name model, but it is still broken. Now I get a
segmentaion fault and webbrick crashes.

class Name < ActiveRecord::Base
set_table_name “name”
set_primary_key “item_number”
belongs_to :order, foreign_key => ‘number’,
:conditions => [‘item_number like
CONCAT(SUBSTRING(order.order_number,1,14),’%’)’]

end
~

Jan P. wrote:

Hi, charlie,

another thing.

:finder_sql isn’t a supported option for belongs_to:

def belongs_to(association_id, options = {})
options.assert_valid_keys(:class_name, :foreign_key, :remote,
:conditions, :order, :include, :dependent, :counter_cache, :extend)

Beyond that :order is kind of a reserved word at least on relationships.
Don’t know if this causes problems as well, but the first one you need
to change is finder_sql.

Regards
Jan

try to

the single quotes were a typo. here is the model

class Name < ActiveRecord::Base
set_table_name “name”
set_primary_key “item_number”
belongs_to :order, :conditions => ‘item_number like
CONCAT(SUBSTRING(bee_order.order_number,1,14),’%’)’

end

I still get a segmentation fault, but I do get this in the log

Processing OrdersController#test (for 192.168.2.28 at 2006-01-17
15:23:17) [GET]
Parameters: {“action”=>“test”, “controller”=>“orders”}
BeeOrder Load (0.032550) SELECT * FROM order WHERE
(order.order_number = ‘90060’) LIMIT 1

the log looks good but the server keeps crashing.

Jan P. wrote:

Why are you providing the :conditions as an array? It should be a string
and you need to escape single quotes or use a mixture of single and
double quotes.

Is there anything happening in your development.log? The first thing you
need is a sql statement in your development.log, once you’ve got that
you could think about workarounds fixing that sql statement.

Jan

maybe it’s a webrick problem. what’s your testing system like? are you
on linux, windows, osx? have you tested another server than webrick?
scgi and apache are simple to set up and you maybe can track the fault
down to webrick. what are the last words of webrick? Is there something
like:

.//script//…//config//…/vendor/rails/actionpack/lib/action_controller/code_generation.rb:68:
[BUG] Segmentation fault
ruby 1.8.2 (2004-12-25) [i386-mswin32]

that let’s you track down where the segmentation fault is happening?

Best Regards
Jan P.

Here is the last info from webbrick, and I have rebooted my linux box to
make sure it’s not the OS.

/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/mysql_adapter.rb:325:
[BUG] Segmentation fault
ruby 1.8.4 (2005-12-24) [i386-linux]

Jan P. wrote:

maybe it’s a webrick problem. what’s your testing system like? are you
on linux, windows, osx? have you tested another server than webrick?
scgi and apache are simple to set up and you maybe can track the fault
down to webrick. what are the last words of webrick? Is there something
like:

.//script//…//config//…/vendor/rails/actionpack/lib/action_controller/code_generation.rb:68:
[BUG] Segmentation fault
ruby 1.8.2 (2004-12-25) [i386-mswin32]

that let’s you track down where the segmentation fault is happening?

Best Regards
Jan P.

resend, because ruby-forum seems to have problems with html messages…


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

both didn’t came through to ruby-forum.com, so I’ll have another try
through ruby-forum itself…

The code fragment in question is:

320 def select(sql, name = nil)
321 @connection.query_with_result = true
322 result = execute(sql, name)
323 rows = []
324 if @null_values_in_each_hash
325 result.each_hash { |row| rows << row }
326 else
327 all_fields = result.fetch_fields.inject({}) { |fields,
f| fields[f.name] = nil; fields }
328 result.each_hash { |row| rows <<
all_fields.dup.update(row) }
329 end
330 result.free
331 rows
332 end

83 def initialize(connection, logger, connection_options, config)
84 super(connection, logger)
85 @connection_options, @config = connection_options, config
86 @null_values_in_each_hash = Mysql.const_defined?(:VERSION)
87 connect
88 end

The adapter tries to iterate of the returned rows. Have you already
tried to execute the sql statement you found in your log on the mysql
command line?

Best Regards
Jan P.

yes it works fine from the command line.

Hi, charlie,

ok. But rails seems to have problems with the returned resultset. Here’s
what I would do:

Get a source version of rails in your_project/vendor/rails which is used
instead of the installed gems with

rake freeze_edge (or freeze to a specific version, look at rake --tasks)

and debug the segmentation fault. Something seems to be wrong with your
results. After changes on
vendor/rails/activerecord/lib/active_record/connec
tion_adapters/mysql_adapter.rb you might have to restart webrick (no big
deal since it crashes anyway ;^)

Best Regards
Jan P.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs