How do Rails ActiveRecord relationship helpers allow methods such as `user.groups.new`?

后端 未结 2 1510
别跟我提以往
别跟我提以往 2021-01-25 17:22

In Rails, I have created a Model that retrieves users from an LDAP database rather than from ActiveRecord. Now I am attempting to integrate my ActiveRecord models with the LDAP-

相关标签:
2条回答
  • 2021-01-25 17:53

    I would go about doing this by peeking into the Rails Source Code, e.g. the code for the Group.create example above can be found in

    http://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html

      def create(attributes = nil, options = {}, &block)
        if attributes.is_a?(Array)
          attributes.collect { |attr| create(attr, options, &block) }
        else
          object = new(attributes, options, &block)
          object.save
          object
        end
      end
    end
    
    0 讨论(0)
  • 2021-01-25 18:08

    Though user.groups appears to be an array of groups, it's actually an entirely separate class -- a Rails internal class that you usually don't know much about called an association proxy. The proxy responds to methods like <<, create, new and so on by proxying requests to the target class and then setting the association appropriately.

    If you want similar functionality you'll have to implement your own kind of proxy associations. Doing so will be pretty complicated, but this might get you started.

    module LDAP
      class Association
        attr_accessor :source, :target
    
        def initialize(source, target)
          @source = source
          @target = target
        end
    
        def <<(obj)
          @source.group_ids = [group_ids + obj].flatten.uniq
          @source.save
        end
    
      end
    end
    
    class User
      def groups
        LDAP::Association.new(self, Group)
      end
    end
    

    This is not even particularly close to how ActiveRecord implements association proxies. However, this is quite a bit simpler than ActiveRecord's solution and should be enough to duplicate some basic ActiveRecord functionality.

    0 讨论(0)
提交回复
热议问题