Overriding association getters

570 views
Skip to first unread message

Мурыгин Антон

unread,
Jun 29, 2015, 9:55:28 AM6/29/15
to rubyonra...@googlegroups.com
Hi! I saw that i documentation here - http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

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 named Project::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.


Now this is my model
class Business < ActiveRecord::Base
  belongs_to
:category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'

  def category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
? Am i missing something?

Dave Aronson

unread,
Jun 29, 2015, 10:05:42 AM6/29/15
to rubyonra...@googlegroups.com
On Monday, June 29, 2015, Мурыгин Антон <mib...@gmail.com> wrote:

class Business < ActiveRecord::Base
  belongs_to
:category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'

  def category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
?

Possibly because AR::Base has no idea about your renaming of that association.  Try calling that method business_category instead, or call business_category instead of super.  (Just don't do both,)


--
Sent from Gmail Mobile; please excuse top posting, typos, etc. :-(

Мурыгин Антон

unread,
Jun 29, 2015, 10:54:19 AM6/29/15
to rubyonra...@googlegroups.com
Hey, but i did not rename it. My model hasn't methods called busyness_category! I though AR generates them according to first argument to 'belongs_to' method, rather than from foreign key name

понедельник, 29 июня 2015 г., 17:05:42 UTC+3 пользователь Dave Aronson написал:

Frederick Cheung

unread,
Jun 29, 2015, 1:00:27 PM6/29/15
to rubyonra...@googlegroups.com


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 category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
? Am i missing something?
 
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-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.

Мурыгин Антон

unread,
Jun 29, 2015, 2:40:58 PM6/29/15
to rubyonra...@googlegroups.com
My rails version is 4.2.2.

понедельник, 29 июня 2015 г., 20:00:27 UTC+3 пользователь Frederick Cheung написал:


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 category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
? Am i missing something?
 
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.

Frederick Cheung

unread,
Jun 30, 2015, 7:24:02 AM6/30/15
to rubyonra...@googlegroups.com, mib...@gmail.com


On Monday, June 29, 2015 at 7:40:58 PM UTC+1, Мурыгин Антон wrote:
My rails version is 4.2.2.


Weird - I created a fresh rails app, added a business model like yours and it works fine. I'd recommended doing the same and seeing whether you can isolate what it is about your app that is different (for example is Business::GeneratedAssociationMethods in the ancestor chain for business? What does Profile::GeneratedAssociationMethods.instance_methods look like?)

Fred

Мурыгин Антон

unread,
Jul 3, 2015, 9:12:40 AM7/3/15
to rubyonra...@googlegroups.com, mib...@gmail.com
Oh yeah, this is because of one gem i use that extends AR in that way. Thanks that you set up fresh app, that helped me.

вторник, 30 июня 2015 г., 14:24:02 UTC+3 пользователь Frederick Cheung написал:
Reply all
Reply to author
Forward
0 new messages