Rails ActiveRecord查询错误的模型

时间:2014-07-07 19:11:36

标签: ruby-on-rails ruby activerecord

在索引操作的控制器中:

    @keys = Quran::Ayah.fetch_ayahs(params[:surah], range.first, range.last).map{|k| k.ayah_key}

    # @cardinalities = Quran::Surah.first
    @cardinalities = Content::Resource.fetch_cardinality(params[:quran])

当我离开这两个时,我在控制台中得到这个:

Quran::Ayah Load (0.6ms)  SELECT "quran"."ayah".* FROM "quran"."ayah"  WHERE (quran.ayah.surah_id = '1' AND quran.ayah.ayah_num >= '1' AND quran.ayah.ayah_num <= '5')  ORDER BY quran.ayah.surah_id, quran.ayah.ayah_num
Content::Resource
  Content::Resource Load (0.6ms)  SELECT "quran"."resource".* FROM "quran"."resource" JOIN content.resource_api_version  using ( resource_id )
PG::UndefinedTable: ERROR:  relation "quran.resource" does not exist
LINE 1: SELECT "quran"."resource".* FROM "quran"."resource" JOIN con...
                                         ^
: SELECT "quran"."resource".* FROM "quran"."resource" JOIN content.resource_api_version  using ( resource_id )
  Rendered bucket/ayat/index.jbuilder (2.3ms)

当我评论@keys行时,它可以正常工作:

Content::Resource Load (3.7ms)  SELECT "content"."resource".* FROM "content"."resource" JOIN content.resource_api_version  using ( resource_id )

不知道为什么会这样?当模型不是那个时,为什么要查询quran.resource

型号:

class Content::Resource < ActiveRecord::Base
    extend Content

    self.table_name = 'resource'
    self.primary_key = 'resource_id'

    belongs_to :author, class_name: 'Content::Author'
    belongs_to :source, class_name: 'Content::Source'
    belongs_to :language, class_name: 'I18n::Language', foreign_key: 'language_code'

    # maybe make the block below a polymorphic class, accessible via 'content' or something
    # dunno wth polymorphic relationships really are, so maybe not -- fancy crap not worth a brain cell
    has_many :_image, class_name: 'Quran::Image', foreign_key: 'resource_id'
    has_many :_text, class_name: 'Quran::Text', foreign_key: 'resource_id'
    has_many :_word_font, class_name: 'Quran::WordFont', foreign_key: 'resource_id'
    has_many :_tafsir, class_name: 'Content::Tafsir', foreign_key: 'resource_id'
    has_many :_translation, class_name: 'Content::Translation', foreign_key: 'resource_id'
    has_many :_transliteration, class_name: 'Content::Transliteration', foreign_key: 'resource_id'
    has_one  :_resource_api_version, class_name: 'Content::ResourceAPIVersion', foreign_key: 'resource_id'


    def self.fetch_cardinality(params_quran)
        Rails.logger.info Content::Resource
        @results = Content::Resource
        .joins('JOIN content.resource_api_version  using ( resource_id )')
        # .select([:sub_type, :cardinality_type, :language_code, :slug, :is_available, :description, :name].map{ |term| "content.resource.#{term}" }.join(', '))
        # .where("content.resource.is_available = 't' AND content.resource.type = 'quran'")
        # .where("content.resource_api_version.v2_is_enabled = 't' ")
        # .where("content.resource.resource_id = ?", params_quran)
        return @results
    end
end


class Quran::Ayah < ActiveRecord::Base
    extend Quran

    self.table_name = 'ayah'
    self.primary_key = 'ayah_key'

    belongs_to :surah, class_name: 'Quran::Surah'

    has_many :words, class_name: 'Quran::Word', foreign_key: 'ayah_key'

    has_many :tokens, class_name: 'Quran::Token', through: :words
    has_many :stems, class_name:  'Quran::Stem',  through: :words
    has_many :lemmas, class_name: 'Quran::Lemma', through: :words
    has_many :roots, class_name:  'Quran::Root',  through: :words

    has_many :_tafsir_ayah, class_name: 'Content::TafsirAyah', foreign_key: 'ayah_key'

    has_many :audio, class_name: 'Audio::File', foreign_key: 'ayah_key'
    has_many :texts, class_name: 'Quran::Text', foreign_key: 'ayah_key'
    has_many :images, class_name: 'Quran::Image', foreign_key: 'ayah_key'
    has_many :glyphs, class_name: 'Quran::WordFont', foreign_key: 'ayah_key'
    has_many :tafsirs, class_name: 'Content::Tafsir', through: :_tafsir_ayah
    has_many :translations, class_name: 'Content::Translation', foreign_key: 'ayah_key'
    has_many :transliterations, class_name: 'Content::Transliteration', foreign_key: 'ayah_key'


    def self.fetch_ayahs(surah_id, from, to)
        self
        .where("quran.ayah.surah_id = ? AND quran.ayah.ayah_num >= ? AND quran.ayah.ayah_num <= ?", surah_id, from, to)
        .order("quran.ayah.surah_id, quran.ayah.ayah_num")
    end
end

更新:

如果有帮助,我偶尔会收到此错误:

  

单表继承机制无法找到子类:'content'。引发此错误是因为列'type'保留用于在继承的情况下存储类。如果您不打算将此列用于存储继承类或覆盖Content :: Resource.inheritance_column以使用另一列来获取该信息,请重命名此列。

0 个答案:

没有答案