Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:41:27+00:00 2026-06-08T23:41:27+00:00

In Rails, I have created a Model that retrieves users from an LDAP database

  • 0

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-based models, so I am writing methods in my models that emulate some common ActiveRecord methods.

One of the methods I am trying to emulate is one that is normally created by the has_many through relationship on ActiveRecord. In ActiveRecord, this relationship would allow the following:

user = User.first
groups = user.groups # == Array of Groups
groups << Group.create(name: "Test") # How does Rails allow this?

How exactly does Rails allow this? I’ve tried dynamically assigning methods to the array instance returned by user.groups, but there doesn’t seem to be any way to make those methods aware of which user record the array was created from. (So they can assign user_id on the new relationship record.) What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:41:30+00:00Added an answer on June 8, 2026 at 11:41 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In rails 3, I have a database model that has a birthday field, of
I have created model, controller and view with rails scaffold generator: rails g scaffold
I'm developing a booking system in Rails 3.1. I have created a model for
I have created a table in my Ruby on Rails application that I am
I have a rails model that I am adding validations to and I seem
I have a Rails 3 model D that belongs to another model, which belongs
I am running a MySQL database in rails 2.3. my model associations that I
I have created a model with several fields that should accept the same data
I have a Tournament model that needs 0, 1, or 2 contacts. I created
I have created a form that needs to show data from 2 tables (parent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.