I have a 2-part question here that's a little tricky and starting to
make my head hurt.

I'm working on a plugin for personal use, where I have an rclients
table (not named clients because of conflicts with another plugin),
and several other models that can have Rclients (polymorphic
association).  I've defined a method acts_as_client_entity that will
set up the necessary associations.

Let me first give my functioning code:

###############  join model:
class ClientEntityAssociation < ActiveRecord::Base
  belongs_to :rclient
  belongs_to :entity, :polymorphic=>true
end

################ plugin:
module ClientEntity
  module ClassMethods
      def acts_as_client_entity
 
has_many :client_entity_associations,  :as=>:entity, :dependent=>:destroy
 
has_many :rclients,                    :through=>    :client_entity_associations
 
has_one  :primary_client,              :through=>    
:client_entity_associations,
                                               :source
=>    :rclient, :conditions=>["client_entity_associations.primary
= ?",true]

        klass = self.name.tableize
        Rclient.class_eval
"has_many :#{klass}, :through=>:client_entity_associations"

        define_method("primary_client=") do |rclient|
          client_entity_associations.update_all("`primary` = false")
          assoc =
client_entity_associations.find_by_rclient_id(rclient.id)
          if assoc && !assoc.primary
            assoc.update_attribute :primary,true
          else
 
client_entity_associations.create(:rclient_id=>rclient.id,:primary=>true)
          end
        end
      end
  end
end
=============================================================

The first part of my question is whether there is a way to pass in a
reference to the calling class (klass) without first setting it as a
variable.  My understand is that if I used self directly in the
class_eval statement, it would evaluate to Rclient.

The second part of my questions is why I had to use define_method for
primary_client= (as opposed to def primary_client=).  I kept getting
conflicts with the dynamic methods rails created from the statement

 has_one :primary_client . . .

I would just like to know why define_method seems to be a little more
forceful.

My apologies if this is a little unwieldy of a question, and I'm open
to any comments/criticism on the general approach.

 - kevin

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to