0

I have the following HABTM relation and corresponding logic that is identical in at least 3 different tables, so I wanted to factor it, and related methods, into a common base class. The problem is, even though the association does show up in the object's association_cache, trying to access it results in an exception (ArgumentError comparison of nil object with a string). I'm confused about why this isn't working, as this seems like basic OOP programming.

My base model is set up like this:

class ProfileItem < ActiveRecord::Base
  self.abstract_class = true
  has_and_belongs_to_many :profiles
  attr_accessor :profile_ids
  before_destroy :clean_up
  before_save :update_profiles

def get_profile_names
  self.profiles.each do |p|  # << exception here
  ...
4

1 回答 1

0

Look into a concept called concerns, introduced in Rails 4. Abstracting a class inherited from ActiveRecord::Base is a recipe for trouble.

A good explanation How to use concerns in Rails 4.

于 2014-05-20T03:59:59.807 回答