Has_many :through & << & STI

I’ve got a pretty crazy has_many :through going.

My join table is set up like this:

Table name: memberships

id :integer not null, primary key

member_of_id :integer

member_of_type :string(255)

user_id :integer

And in my User model, I’ve got:
class User < ActiveRecord::Base
has_many :memberships
has_many :terms, :through => :memberships, :source
=> :term, :conditions => “memberships.member_of_type = ‘Term’”

def add_term(term)
unless terms.exists? [“memberships.member_of_type = ? AND
memberships.member_of_id = ?”, “Term”, term]
terms << term #Line 50
end
return true
end

end

Any time I call terms << term (in this method or in the console or
whatever) I’m getting:

NoMethodError (You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.constantize):
(eval):7:in before_save' /vendor/rails/activerecord/lib/active_record/callbacks.rb:301:insend’
/vendor/rails/activerecord/lib/active_record/callbacks.rb:301:in
callback' /vendor/rails/activerecord/lib/active_record/callbacks.rb:206:increate_or_update’
/vendor/rails/activerecord/lib/active_record/base.rb:2151:in
save_without_validation!' /vendor/rails/activerecord/lib/active_record/validations.rb:927:insave_without_dirty!’
/vendor/rails/activerecord/lib/active_record/dirty.rb:82:in
save_without_transactions!' /vendor/rails/activerecord/lib/active_record/transactions.rb: 110:insave!’
/vendor/rails/activerecord/lib/active_record/connection_adapters/
abstract/database_statements.rb:66:in transaction' /vendor/rails/activerecord/lib/active_record/transactions.rb:79:intransaction’
/vendor/rails/activerecord/lib/active_record/transactions.rb:98:in
transaction' /vendor/rails/activerecord/lib/active_record/transactions.rb: 110:insave!’
/vendor/rails/activerecord/lib/active_record/transactions.rb:
118:in rollback_active_record_state!' /vendor/rails/activerecord/lib/active_record/transactions.rb: 110:insave!’
/vendor/rails/activerecord/lib/active_record/validations.rb:898:in
create!' /vendor/rails/activerecord/lib/active_record/associations/ has_many_through_association.rb:60:in<<’
/vendor/rails/activerecord/lib/active_record/base.rb:1801:in
with_scope' /vendor/rails/activerecord/lib/active_record/associations/ has_many_through_association.rb:60:insend’
/vendor/rails/activerecord/lib/active_record/associations/
has_many_through_association.rb:60:in <<' /vendor/rails/activerecord/lib/active_record/associations/ has_many_through_association.rb:56:ineach’
/vendor/rails/activerecord/lib/active_record/associations/
has_many_through_association.rb:56:in <<' /vendor/rails/activerecord/lib/active_record/connection_adapters/ abstract/database_statements.rb:66:intransaction’
/vendor/rails/activerecord/lib/active_record/transactions.rb:79:in
transaction' /vendor/rails/activerecord/lib/active_record/associations/ has_many_through_association.rb:55:in<<’
/app/models/user.rb:50:in `add_term’

Anyone have any idea’s? I’m running edge rails from last week.

Thanks,
BJ

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs