与 Ruby、Redis 和 Ohm 的多对多关系 [英] Many-to-many relationships with Ruby, Redis, and Ohm

查看:48
本文介绍了与 Ruby、Redis 和 Ohm 的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ohm 在 Redis 中创建多对多关系.例如,我定义了 Book 和 Author 模型,如下所示:

class Book <欧姆::型号属性:标题设置:作者,作者结尾类作者 <欧姆::型号属性:姓氏属性:first_name设置:书籍,书籍结尾

我希望能够做的是利用 Ohm 的索引功能来进行查找,例如:

需要'test_helper'类 ManyToManyRelationshipTest <ActiveSupport::TestCase设置做@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结尾测试按作者查找一本书"做assert Book.find(:author_id => @andy_hunt.id).include?(@pick_axe)assert Book.find(:author_id => @andy_hunt.id).include?(@thinking_and_learning)结尾测试按书查找作者"做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)结尾结尾

使用上面的代码,我得到以下异常:Ohm::Model::IndexNotFound: 索引 :author_id 未找到.(尝试查找给定作者的书籍时)

我尝试按照此处所述构建自定义索引:http://ohm.keyvalue.org/examples/tagging.html 和这里:http://pinoyrb.org/红宝石/欧姆内部技巧

不幸的是,看起来索引是在第一次创建模型时构建的,这意味着 Set 是空的(因为,如果我理解正确,在为模型分配 ID 之前,Set 无法在 Ohm 中使用).

我非常感谢任何帮助或建议!

解决方案

这种情况下的解决方案不太自动:

需要欧姆"课本<欧姆::型号attr_accessor :作者属性:标题索引:作者结尾类作者 <欧姆::型号属性:名称结尾###需要测试/单元"类 BooksTest <测试::单元::测试用例def test_books_by_authordave = Author.create(name: "Dave")andy = Author.create(name: "Andy")dhh = Author.create(名称:DHH")镐 = Book.create(title: "镐", 作者: [dave.id, andy.id])assert_equal 镐,Book.find(authors: dave.id).firstassert_equal 镐,Book.find(authors: andy.id).firstassert_equal nil, Book.find(authors: dhh.id).first结尾结尾

有意义吗?

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!

解决方案

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?

这篇关于与 Ruby、Redis 和 Ohm 的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆