Rails is misinterpreting model name

I have two models

class Ferm < AR::Base
  has_many :daily_analyses
end

class DailyAnalysis < AR::Base
  belongs_to :ferm
end

$ script/console
Loading development environment (Rails 2.0.2)

f = Ferm.find(:first)

=> #<Ferm ... >

f.daily_analyses

NameError: uninitialized constant Ferm::DailyAnalyAsis
  from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:478:in `const_missing'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:1750:in `compute_type'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
reflection.rb:125:in `send'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
reflection.rb:125:in `klass'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
associations/has_many_association.rb:152:in `construct_sql'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
associations/has_many_association.rb:6:in `initialize'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
associations.rb:1032:in `new'
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
associations.rb:1032:in `daily_analyses'
  from (irb):3

For some reason Rails is converting "daily_analyses" to
"daily_analy_asis". Using the pluralizer and tableizer for
"daily_analysis" correctly give "daily_analysis".

Should I roll back my schema and rename the table something other than
"daily_analysis?"

Thanks --SH

Hmmm, that's weird.
After successfully reproducing your problem, I added the following to
config/initializers/inflections.rb

Inflector.inflections do |inflect|
  inflect.singular 'daily_analyses', 'daily_analysis'
end

Now, in the console:

"daily_analyses".singularize # returns "daily_analysis"
"daily_analyses".classify #returns DailyAnalysis

But calling the association on Ferm still yields an error.

:open_mouth:

Giuseppe

shenry wrote:

Giuseppe Bertini wrote:

Hmmm, that's weird.
After successfully reproducing your problem, I added the following to config/initializers/inflections.rb

Inflector.inflections do |inflect|
  inflect.singular 'daily_analyses', 'daily_analysis'
end

Shouldn't that be

    inflect.singular 'analyses', 'analysis'
?

You can avoid inflection hassles entirely and just explicitly tell it
which class to use:

class Ferm < AR::Base
  has_many :daily_analyses, :class_name => "DailyAnalysis"
end

class DailyAnalysis < AR::Base
  belongs_to :ferm
end

with :

Inflector.inflections do |inflect|
  inflect.irregular 'daily_analysis', 'daily_analyses'
end

I get ...

"daily_analyses".classify

=> "DailyAnalysis"

ferm.daily_analyses

=> NameError: uninitialized constant Ferm::DailyAnalyAsis (...etc...)

"daily_analyses".camelize

=>"DailyAnalyses" # shouldn't this camelize to 'DailyAnalysis'?

"daily_analyses".camelize.classify

=>"DailyAnalyAsis"

it seems like when rails is putting together the Ferm::DailyAnalysis
relationship it first camelizes "daily_analyses" into "DailyAnalyses"
then classifies it incorrectly as "DailyAnalyAsis".

I think I'll just change my table name to something like "daily".

SH

shenry wrote:

Inflector.inflections do |inflect|
  inflect.irregular 'daily_analysis', 'daily_analyses'
end

That inflects too much.

Did anyone try my suggestion?

   inflect.irregular 'analysis', 'analyses'

It would be the same for 'daily_crisis' to 'daily_crises'. (-:

@Philp,

I tried your suggestion, same result as the other things I tried.

@Max,

Works great, very simple solution. Thanks,

SH