Overriding generated methods
Association methods are generated in a module that is included into the model class, which allows you to easily override with your own methods and call the original generated method with
super
. For example:class Car < ActiveRecord::Base belongs_to :owner belongs_to :old_owner def owner=(new_owner) self.old_owner = self.owner super end end
If your model class is
Project
, the module is namedProject::GeneratedFeatureMethods
. The GeneratedFeatureMethods module is included in the model class immediately after the (anonymous) generated attributes methods module, meaning an association will override the methods for an attribute with the same name.
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'
def category super || BusinessCategory.new(name: 'other') endend
super: no superclass method `category' for #<Business:0x000001023014b8>
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing?
super: no superclass method `category' for #<Business:0x000001023014b8>
Now this is my model
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing? Am i missing something?
super: no superclass method `category' for #<Business:0x000001023014b8>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fabf3f0c-db07-44b4-a04c-0b060eb9eaa5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Monday, June 29, 2015, Мурыгин Антон <mib...@gmail.com> wrote:Now this is my model
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing? Am i missing something?
super: no superclass method `category' for #<Business:0x000001023014b8>
Which version of rails?Fred
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
My rails version is 4.2.2.