#802 ✓resolved
Tomoaki Hayasaka

uninitialized constant is caused by belongs_to association with non-default :class_name

Reported by Tomoaki Hayasaka | September 25th, 2010 @ 11:04 PM | in Hobo 1.3 (Rails 3)

I use Hobo 1.3pre6 + Rails 3.0.

When I add belongs_to association with non-default :class_name to a model, and it is a parent relationship in the view hints, the application doesn't start up but uninitialized constant is raised.

For example, the agility demo already has belongs_to with non-default :class_name

class Project < ActiveRecord::Base
  hobo_model # Don't put anything above this
...
  belongs_to :owner, :class_name => "User", :creator => true
...

and adding a view hint

class ProjectHints < Hobo::ViewHints
...
  parent :owner # add this
end

results

/var/lib/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/inflector/methods.rb:124:in `block in constantize': uninitialized constant Owner (NameError)
      from /var/lib/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/inflector/methods.rb:123:in `each'
      from /var/lib/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/inflector/methods.rb:123:in `constantize'
      from /var/lib/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/core_ext/string/inflections.rb:43:in `constantize'
      from rapid/pages:105:in `block in run_one'

Here is a (rather dirty) fix for this issue:

diff --git a/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb b/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb
index 196bc34..bed6660 100644
--- a/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb
+++ b/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb
@@ -101,7 +101,8 @@ model_key = model.name.tableize
 
 <%
 back_link      = model.view_hints.parent
-back_link_human_name  = back_link.to_s.singularize.camelize.constantize.model_name.human unless back_link.blank?
+back_link_human_name = model.reflections[back_link] && model.reflections[back_link].klass unless back_link.blank?
+back_link_human_name = back_link_human_name && back_link_human_name.model_name.human
 boolean_fields = model.columns.select { |c| c.type == :boolean }.*.name - model.view_hints.inline_booleans
 creator        = model.creator_attribute
 creator_link   = creator && model.reflections[creator] && linkable?(model.reflections[creator].klass, :show)
@@ -138,7 +139,7 @@ end
 <% end -%>
           <header param="content-header">
 <% if back_link -%>
-            <a:<%= back_link %> param="parent-link">&laquo; <ht key="<%= model_key %>.actions.back_to_parent" parent="&<%= back_link.to_s.singularize.camelize %>.model_name.human" name="&this">Back to <name/></ht></a:<%= back_link %>>
+            <a:<%= back_link %> param="parent-link">&laquo; <ht key="<%= model_key %>.actions.back_to_parent" parent="<%= back_link_human_name %>" name="&this">Back to <name/></ht></a:<%= back_link %>>
 <% end -%>
             <h2 param="heading">
               <ht key="<%= model_key %>.show.heading" name="&this.respond_to?(:name) ? this.name : ''">

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Tags

Pages