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

后端 未结 2 1511
别跟我提以往
别跟我提以往 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
    

提交回复
热议问题