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 9285463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:10:21+00:00 2026-06-18T19:10:21+00:00

I’m trying to integrate Redis into a Rails app to replace has_many through relations.

  • 0

I’m trying to integrate Redis into a Rails app to replace “has_many through” relations.
I’d like to do that seamlessly so we don’t have to change the code through out the app.
My idea is to override different methods of some class attributes (the followers attribute of the class Speaker for example) to be able to create custom behavior when using them:
Here are behaviors I’d like to get to:

s = Speaker.new
s.followers # calls custom getter and returns [User1, User2]
s.followers << User.create
s.followers # calls custom getter and returns [User1, User2, User3]

Here is my idea inspired by Overriding instance variable array's operators in Ruby

class Speaker < ActiveRecord::Base  
  attr_accessor :followers

  def initialize
    super
    @followers = []
    class << @followers
      def <<(u)
        puts "setter #{u.id}" 
        $redis.set "speaker#{self.id}followers", u.id
        super(u.id)
      end
    end
  end

  def followers
    puts "getter"
    user_ids = $redis.get "speaker#{self.id}followers"
    User.find_all user_ids
  end

end

The problem is that the implementation of the followers getter override the implementation of “def <<(val)”

if the getter “def followers” is not defined:

s.followers
# []
s.followers << User.create 
# "setter 1"
# [1]
s.followers
# [1]
s.followers << User.create 
# "setter 2"
# [1, 2]
s.followers
# [1, 2]

if the getter “def attendees” is defined:

s.followers << User.create
# ["My", "Custom", "Array", User1]
s.followers
# ["My", "Custom", "Array"]
s.followers << User.create
# ["My", "Custom", "Array", User2]
s.followers
# ["My", "Custom", "Array"]

How could I get the getter and the setter “<<” to work together?

  • 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-18T19:10:23+00:00Added an answer on June 18, 2026 at 7:10 pm

    Your problem here is that the getter is returning a new array. You modify the singleton class of the @followers array, but that is not being used in the getter:

    def followers
      puts 'getter'
      ['a','new','array']
    end
    

    If you want to have a custom getter, then you need to make sure that the getter returns @followers (without changing the underlying reference), or you need to re-decorate the array.

    However, what AlexWayne suggested is the proper way to do this. Return a proxy object that handles the redis details:

    class FollowersList < SimpleDelegator
      def initialize(assoc)
        @assoc = assoc
        super(_followers)
      end
    
      def _reload
        __setobj__ _followers
        self
      end
    
      def _followers
        user_ids = $redis.get key
        User.find_all user_ids
      end
    
      def _key
        "speaker#{@assoc.id}followers"
      end
    
      # implement your overrides. The _reload method is to force the list to sync
      # with redis again, there are other ways to do this that wouldn't do the query
      # again
      def <<(val)
        $redis.lpush key, val.id
        _reload
      end
    
      #etc
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.