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

  • Home
  • SEARCH
  • 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 6856259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:46+00:00 2026-05-27T01:51:46+00:00

I’m attempting to create a many-to-many relationship in Redis using Ohm. As an example,

  • 0

I’m attempting to create a many-to-many relationship in Redis using Ohm. As an example, I have Book and Author models defined as follows:

class Book < Ohm::Model
  attribute :title
  set :authors, Author
end

class Author < Ohm::Model
  attribute :last_name
  attribute :first_name
  set :books, Book
end

What I would like to be able to do is leverage Ohm’s indexing capabilities to do finds such as:

require 'test_helper'

class ManyToManyRelationshipTest < ActiveSupport::TestCase

  setup do
    @dave_thomas = FactoryGirl.build(:dave_thomas)
    @andy_hunt = FactoryGirl.build(:andy_hunt)
    @chad_fowler = FactoryGirl.build(:chad_fowler)

    @pick_axe = FactoryGirl.build(:pick_axe)
    @pick_axe.authors << @dave_thomas 
    @pick_axe.authors << @andy_hunt
    @pick_axe.authors << @chad_fowler

    @thinking_and_learning = FactoryGirl.build(:pragmatic_thinking_and_learning)
    @thinking_and_learning.authors << @andy_hunt
  end

  test "find a Book by Author" do
    assert Book.find(:author_id => @andy_hunt.id).include?(@pick_axe)
    assert Book.find(:author_id => @andy_hunt.id).include?(@thinking_and_learning)
  end

  test "find Authors by Book" do
    assert Author.find(:book_id => @pick_axe.id).include?(@dave_thomas)
    assert Author.find(:book_id => @pick_axe.id).include?(@andy_hunt)
    assert Author.find(:book_id => @pick_axe.id).include?(@chad_fowler)
  end
end

With the code above, I get the following Exception:
Ohm::Model::IndexNotFound: Index :author_id not found. (when trying to find Books given an author)

I’ve tried to build custom indices as described here: http://ohm.keyvalue.org/examples/tagging.html, and here: http://pinoyrb.org/ruby/ohm-inside-tricks

Unfortunately, it looks like the index is built when the model is first created, which means the Set is empty (because, if I understand correctly, Sets are unusable in Ohm until the model has been assigned an ID).

I really appreciate any help or suggestions!

  • 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-05-27T01:51:46+00:00Added an answer on May 27, 2026 at 1:51 am

    The solution in this case is a bit less automatic:

    require "ohm"
    
    class Book < Ohm::Model
      attr_accessor :authors
    
      attribute :title
    
      index :authors
    end
    
    class Author < Ohm::Model
      attribute :name
    end
    
    ###
    
    require "test/unit"
    
    class BooksTest < Test::Unit::TestCase
      def test_books_by_author
        dave = Author.create(name: "Dave")
        andy = Author.create(name: "Andy")
        dhh = Author.create(name: "DHH")
    
        pickaxe = Book.create(title: "Pickaxe", authors: [dave.id, andy.id])
    
        assert_equal pickaxe, Book.find(authors: dave.id).first
        assert_equal pickaxe, Book.find(authors: andy.id).first
    
        assert_equal nil, Book.find(authors: dhh.id).first
      end
    end
    

    Makes sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.