Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Ruby、Redis和Ohm的多对多关系_Ruby_Redis_Ohm - Fatal编程技术网

与Ruby、Redis和Ohm的多对多关系

与Ruby、Redis和Ohm的多对多关系,ruby,redis,ohm,Ruby,Redis,Ohm,我正在尝试使用Ohm在Redis中创建多对多关系。例如,我将Book和Author模型定义如下: class Book < Ohm::Model attribute :title set :authors, Author end class Author < Ohm::Model attribute :last_name attribute :first_name set :books, Book end 教材

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

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

class Author < Ohm::Model
  attribute :last_name
  attribute :first_name
  set :books, Book
end
教材
我希望能够利用Ohm的索引功能进行查找,例如:

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
需要“测试助手”
类ManyToManyRelationshipTest@pick\u axe.id)。包括?(@chad\u fowler)
结束
结束
使用上面的代码,我得到以下异常: Ohm::Model::IndexNotFound:索引:找不到作者id。(当试图查找作者提供的书籍时)

我已尝试按如下所述构建自定义索引:,此处:

不幸的是,看起来索引是在第一次创建模型时建立的,这意味着集合是空的(因为,如果我理解正确的话,集合在Ohm中是不可用的,直到模型被分配了一个ID)


我真的很感谢任何帮助或建议

这种情况下的解决方案自动化程度较低:

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
需要“欧姆”
教材<欧姆::模型
属性访问器:作者
属性:标题
索引:作者
结束
类作者

有意义吗?

这种情况下的解决方案自动化程度稍低:

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
需要“欧姆”
教材<欧姆::模型
属性访问器:作者
属性:标题
索引:作者
结束
类作者
有道理吗