Mongoid和ActiveRecord的关系:未定义的方法`quoted_table_name“ [英] Mongoid and ActiveRecord relations: undefined method `quoted_table_name'

查看:244
本文介绍了Mongoid和ActiveRecord的关系:未定义的方法`quoted_table_name“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类竞赛<的ActiveRecord :: Base的
  HAS_ONE:claim_template
结束

类ClaimTemplate
  包括Mongoid ::文档
  belongs_to的:大赛
结束

# 安慰
Contest.new.claim_template
#=> NoMethodError:未定义的方法`quoted_table_name'的ClaimTemplate:类
 

好了,让我们添加 quoted_table_name ClaimTemplate

 高清self.quoted_table_name
  claim_templates
结束

# 安慰
Contest.new.claim_template
#=>零
# 凉!
# 但:
Contest.last.claim_template
#=>类型错误:无法转换符号转换成String
 

那么,如何能配置我的模型与对方正常工作

PS:

现在我有这样的结构,它工作得很好,但我想有关系的好处( Assosiations )。

 类竞赛<的ActiveRecord :: Base的
  #HAS_ONE:claim_temlate
  高清claim_template
    ClaimTemplate.where(:contest_id => self.id)。首先
  结束

  #Mongoid将是疯了没有这个黑客
  高清self.using_object_ids?
    假
  结束
结束
 

解决方案

我不知道这是否已经正式尚未实施。协会通过的ActiveRecord ::反射,这是很难coded到关系表的想法来处理居多,看到这个类:

什么这表明的是,有没有办法ActiveRecord关联可以像Mongoid工作。

我建议,要么建立一个创业板来解决这个问题,通过建立一个类似的反映包装Mongoid,或者只是手动构建相关联的对象。

class Contest < ActiveRecord::Base
  has_one :claim_template
end

class ClaimTemplate
  include Mongoid::Document
  belongs_to :contest
end

# console
Contest.new.claim_template
#=> NoMethodError: undefined method `quoted_table_name' for ClaimTemplate:Class

ok, let's add quoted_table_name to ClaimTemplate:

def self.quoted_table_name
  "claim_templates"
end

# console
Contest.new.claim_template
#=> nil
# Cool!
# But:
Contest.last.claim_template
#=> TypeError: can't convert Symbol into String

So how can I configure my models to work properly with each other

PS:

Now I have this construction, which works fine, but I want to have benefits of Relations (Assosiations).

class Contest < ActiveRecord::Base
  # has_one :claim_temlate
  def claim_template
    ClaimTemplate.where(:contest_id => self.id).first
  end

  # Mongoid going to be crazy without this hack
  def self.using_object_ids?
    false
  end
end

解决方案

I am not sure if this has been formally implemented yet. Associations are handled mostly through ActiveRecord::Reflection, which is hardcoded to the idea of relational tables, see this class:

What that's suggesting is that there is no way ActiveRecord associations can work with things like Mongoid.

I recommend either building a gem to solve that problem by building a similar reflection wrapper for Mongoid, or just construct the associated objects manually.

这篇关于Mongoid和ActiveRecord的关系:未定义的方法`quoted_table_name“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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