AR: Undefined attribute method on abstract_class

22 views
Skip to first unread message

itsmechlark

unread,
Dec 2, 2014, 3:03:58 AM12/2/14
to rubyonra...@googlegroups.com

NameError: undefined method `content' for class `LibraryFramework'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/core_ext/module/aliasing.rb:32:in `alias_method'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/core_ext/module/aliasing.rb:32:in `alias_method_chain'

from /Users/itsmechlark/Desktop/GitHub/itsmechlark/cdasiaonline/app/models/library_framework.rb:134:in `<class:LibraryFramework>'

from /Users/itsmechlark/Desktop/GitHub/itsmechlark/cdasiaonline/app/models/library_framework.rb:2:in `<top (required)>'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:443:in `load'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:443:in `block in load_file'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:633:in `new_constants_in'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:442:in `load_file'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:342:in `require_or_load'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:480:in `load_missing_constant'

from /Users/itsmechlark/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:180:in `const_missing'

from /Users/itsmechlark/Desktop/GitHub/itsmechlark/cdasiaonline/app/models/sec.rb:1:in `<top (required)>'

class LibraryFramework < ActiveRecord::Base
self.abstract_class = true

  def content_with_corrector
justified = self.class.justify_content(content_without_corrector)
self.class.check_replace_invalid_format(justified)
end
alias_method_chain :content, :corrector

end

class Sec < LibraryFramework
BANNER = 'Taxation'
MIN_YEAR = 1980

validates :docdate, date: true,
inclusion: {
in: Date.new(MIN_YEAR)..Time.now.to_date,
message: "The date must be between #{Date.new(MIN_YEAR)} and #{Time.now.to_date}"},
:if => lambda{ |object| object.doc_date.present? }
validates :year, :inclusion => { :in => MIN_YEAR..(Time.now.year),
:message => "The year must be between #{MIN_YEAR} and #{Time.now.year}" },
:if => lambda{ |object| object.year.present? }

end


Frederick Cheung

unread,
Dec 2, 2014, 9:02:13 AM12/2/14
to rubyonra...@googlegroups.com

On Tuesday, December 2, 2014 8:03:58 AM UTC, itsmechlark wrote:

class LibraryFramework < ActiveRecord::Base

  self.abstract_class = true

  def content_with_corrector
justified = self.class.justify_content(content_without_corrector)
self.class.check_replace_invalid_format(justified)
end
alias_method_chain :content, :corrector


Well this class doesn't have a content method. If content is an accessor method generated by active record for a database column of the same name then the issue is that the abstract class has no table and this no columns. You might be able to use the inherited hook to add this override when your class is corrected

Also in recent versions of rails the accessor methods are generated in a module included in the class - i don't thing you need to use alias_method_chain anymore - normal inheritance will do the job, so you might be able to get away with just

  def content
    justified = self.class.justify_content(super)
    self.class.check_replace_invalid_format(justified)
  end 

Even if you're not on the right rails version, you can use read_attribute instead of calling super.

Fred
Reply all
Reply to author
Forward
0 new messages