views:

70

answers:

2

In a Rail 3 project I have a script:

<%= javascript_tag do -%>
  var columns = new Array();
  <% for table in @tables -%>
    <% for column in ActiveRecord::Base.const_get(ActiveRecord::Base.class_name(table)).columns -%>
      columns.push(new Array('<%= table %>', '<%= column.name %>'));
    <% end -%>
  <% end -%>
  function mergeTableSelected() {
    var o = $('select#merge_table option:selected');
    $('th select option').remove();
    $('th select').each(function(i, select) {
      select.options.add(new Option());
      $.each(columns, function(j, column) {
        if (o.text() == column[0]) {
          select.options.add(new Option(column[1]));
        }
      });
    });
  }
<% end -%>

After updating from 3.0.0.beta4 to 3.0.0.rc I get:

undefined method `class_name' for ActiveRecord::Base:Class

What to do now?

A: 

They removed it from Rails 3RC:
http://github.com/rails/rails/commit/735a4db6854e73e871e6b01ec003f0670cc5ee14

Not sure why, but you can probably just use a different approach in the view.. it looks like you are pulling all the tables in your project, so you can turn the string into a constant and call .columns on that constant:

  <% @tables.each do |table| %>
    <% table.classify.constantize.columns.each do |column| %>
      columns.push(new Array('<%= table %>', '<%= column.name %>'));
    <% end %>
  <% end %>
cowboycoded
I updated my solution with another method that may work (depending on what @tables is) without using the ActiveRecord::Base methods
cowboycoded
+1  A: 

I solved by problem with:

...
<% klass = table.to_s.classify %>
<% for column in ActiveRecord::Base.const_get("#{klass}").columns %>
...
Arwed