I have this situation:
A ''processable'' model has_many :preprocessors and/or
:postprocessors
I do that:
# @processor_and_processable.id # @processor_and_processable.processable_id #
@processor_and_processable.processable_type #
@processor_and_processable.processor_id #
@processor_and_processable.processor_type #
@processor_and_processable.created_at # @processor_and_processable.updated_at #
@processor_and_processable.lock_version # @processor_and_processable.created_by
# @processor_and_processable.updated_by #
class ProcessorAndProcessable < ActiveRecord::Base
belongs_to :postprocessor, :foreign_key => :processor_id
belongs_to :preprocessor, :foreign_key => :processor_id
belongs_to :processable, :polymorphic => true
end
# @foo.somethingclass Foo < ActiveRecord::Base
has_many :processor_and_processables, :as => :processable
has_many :preprocessors, :through => :processor_and_processables
has_many :postprocessors, :through => :processor_and_processables
end
# @processor.id # @processor.descripcion # @processor.task # @processor.type #
@processor.created_at # @processor.updated_at # @processor.lock_version #
@processor.created_by # @processor.updated_by #
class Processor < ActiveRecord::Base
end
class Postprocessor < Processor
has_many :processor_and_processables,
:foreign_key => "processor_id",
:conditions => [''processor_type
=?'',''Postprocessor'']
has_many :processables, :through => :processor_and_processables
end
class Preprocessor < Processor
has_many :processor_and_processables,
:foreign_key => ''processor_id'',
:conditions => [''processor_type
=?'',''Preprocessor'']
has_many :processables, :through => :processor_and_processables
end
---------------
...later in the console
>foo = Foo.new
>foo.postprocessors
[]>foo.preprocessors
[]>foo.postprocessors << Postprocessor.new(:task =>
"send_mails_to_subscripted_users")
ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot associate
new records through ''Foo#processor_and_processables'' on
''#''. Both
records must have an id in order to create the has_many :through record
associating them.
Help!
Juan Matias.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---