Topic: ActiveRecord not using my alternate table name

I am getting a problem when I use set_table_name in an ActiveRecord model.  I've created a model WpBlogPosts that maps to ob_posts in my test and development databases.  I then try to create a scaffold, but I get the following error:

error  Before updating scaffold from new DB schema, try creating a table for your model (WpBlogPost)

A bit more investigation reveals that at no time is set_table_name being called with my table name; the only call into set_table_name has this stack, and the table name being set is 'wp_blog_posts', not the table I specified.

So...

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1189:in `define_attr_method'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:614:in `set_table_name'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:566:in `reset_table_name'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:561:in `table_name'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:696:in `columns'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1971:in `attributes_from_column_definition'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1353:in `initialize_without_callbacks'
  /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:236:in `initialize'
  /usr/local/lib/ruby/gems/1.8/gems/trestle_generator-1.1.7.3/trestle_generator.rb:191:in `model_instance'
  /usr/local/lib/ruby/gems/1.8/gems/trestle_generator-1.1.7.3/trestle_generator.rb:172:in `create_sandbox'
  /usr/local/lib/ruby/gems/1.8/gems/trestle_generator-1.1.7.3/trestle_generator.rb:85:in `manifest'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/commands.rb:106:in `render_template_part'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/commands.rb:284:in `complex_template'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/manifest.rb:47:in `send_actions'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/manifest.rb:46:in `send_actions'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/manifest.rb:31:in `replay'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/commands.rb:41:in `invoke!'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/scripts/../scripts.rb:31:in `run'
  /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/generate.rb:6
  /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
  /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
  ./script/generate:3

So, what's the cause of this?  Below is the model code (it's pretty empty, since the model is all shiny and new) that is the root of this, can someone tell me why this doesn't work?

class WpBlogPost < ActiveRecord::Base
 
  # : DEVELOPER ATTENTION - determine how to dynamically select this table base.
  set_table_name "ob_posts"
  set_primary_key "ID"
 
  has_many :comments, :class_name => 'WpBlogComment', :foreign_key => 'comment_post_ID', :dependent => true
  has_one :author, :class_name => 'WpUser', :foreign_key => 'post_author'
  has_and_belongs_to_many :categories, :class_name => 'WpPostCategory', :join_table => "ob_post2cat", :foreign_key => "category_id"
 
  def self.find_by_permalink(year,month,day,title)
    find :first, :conditions => ["YEAR(post_date) = ? AND MONTH(post_date) = ? AND DAYOFMONTH(post_date) = ? AND post_name = ?", year, month, day, title]
  end
 
end

Last edited by offby1 (2006-12-18 21:42:20)

Re: ActiveRecord not using my alternate table name

So, can anyone help out here?  I know this is a bit obscure, but I can't be the first person to try to do this (since the method is there).  I'm guessing it's a ruby problem, or something like it; my set_table_name method isn't being invoked *before* the columns method in the base class, so it's not overriding the default name.

Anyone?

Re: ActiveRecord not using my alternate table name

Just to make sure which statement is causing the problem can you comment out everything except the "set_table_name" statement?

Re: ActiveRecord not using my alternate table name

willrl, thanks for the reply;

I tried your suggestion, attempting to create a scaffold with just the set_table_name line in place in the class, and still the same error.

Below is the contents of the development.log file, if it helps.

Any ideas?

--- update

I should add that I have tried this both with Rails 1.1.6 and 1.2 RC1, and the same issue occurs.

Processing InfoController#properties (for 127.0.0.1 at 2006-12-20 08:10:05) [GET]
  Session ID: 8e4250aa09ce32de113a2340fd34f59d
  Parameters: {"action"=>"properties", "controller"=>"rails/info"}
  SQL (0.058686)   SELECT version FROM schema_info
Completed in 0.19959 (5 reqs/sec) | Rendering: 0.00138 (0%) | DB: 0.05869 (29%) | 200 OK [http://localhost/rails/info/properties]


Processing InfoController#properties (for 127.0.0.1 at 2006-12-20 08:14:25) [GET]
  Session ID: 8e4250aa09ce32de113a2340fd34f59d
  Parameters: {"action"=>"properties", "controller"=>"rails/info"}
  SQL (0.000425)   SELECT version FROM schema_info
Completed in 0.08594 (11 reqs/sec) | Rendering: 0.00106 (1%) | DB: 0.00042 (0%) | 200 OK [http://localhost/rails/info/properties]
  WpBlogPost Columns (0.000000)   Mysql::Error: #42S02Table 'rblog_development.wp_blog_posts' doesn't exist: SHOW FIELDS FROM wp_blog_posts

Last edited by offby1 (2006-12-21 21:19:18)

Re: ActiveRecord not using my alternate table name

try creating a new model "ObPost" and use the console to see if you can ObPost.find() something in the table.

Re: ActiveRecord not using my alternate table name

Yeah, that works about as expected; The scaffold created without error, and everything that should be there was there.

So it's (apparently) something in the set_table_name call that's not happening right.

Re: ActiveRecord not using my alternate table name

I haven't used set_table_name myself but this should be working.

One thing you can try: upgrading rails.  right now your app is using the default installed version of Rails.  You can update it to use the cutting-edge version by running:
$ rake rails:freeze:edge
and if that doesn't work any better you can go back to the way things were by:
$ rm -rf ./vendor/rails

also, where did this stack trace come from?  Did you run ./script/generate (that's what it looks like).  What happens if you run your app - is there a stack trace when you try to use your model?

Re: ActiveRecord not using my alternate table name

The stack trace was achieved by... crap, I can't recall the exact process; ./script/generate eats that level of detail, but I did some tinkering about in it to force that to propagate out; probably poked a hole in the error handling code in the scaffold generator to make that happen.  You can get it by creating a new instance of the model object and instantiating it, however.

In short, I forget the exact process I used wink

Let me try that bleeding edge and get back to you.  I could be wrong, though, but hasn't ANYONE used this functionality before?  1.1.6 is supposed to be stable, and I tried with that one first.

Edit

I tested the bleeding edge rails version, and while it didn't make anything worse, it still had the same problem.

At this point, I think I should probably submit a bug somewhere; does that sound like a good idea?

Last edited by offby1 (2006-12-26 17:07:01)