Ruby类属性没有正确查询

时间:2014-03-10 22:35:01

标签: ruby-on-rails ruby activerecord attributes

我的Ruby on Rails项目中有一个班级,我最近必须添加一个属性,:identifier。该类具有自定义元数据字段,用于搜索和索引目的。问题是,我添加的新属性并没有帮助我按照我想要的方式找到对象。如果我尝试使用:identifier查询对象来执行此操作,我将始终获得一个空数组。是的,我已多次检查以确保测试对象确实存在。

这是模型:

class Institution < ActiveFedora::Base
  include Hydra::AccessControls::Permissions

  # NOTE with rdf datastreams must query like so ins = Institution.where(desc_metadata__name_tesim: "APTrust")
  has_metadata "rightsMetadata", type: Hydra::Datastream::RightsMetadata
  has_metadata 'descMetadata', type: InstitutionMetadata

  has_many :intellectual_objects, property: :is_part_of

  has_attributes :name, :brief_name, :identifier, datastream: 'descMetadata', multiple: false

  validates :name, :identifier, presence: true
  validate :name_is_unique
  validate :identifier_is_unique


  def users
    User.where(institution_pid: self.pid).to_a.sort_by(&:name)
  end


  private

  def name_is_unique
    errors.add(:name, "has already been taken") if Institution.where(desc_metadata__name_ssim: self.name).reject{|r| r == self}.any?
  end

  def identifier_is_unique
    count = 0;
    Institution.all.each do |inst|
      count += 1 if inst.identifier == self.identifier
    end
    if(count > 0)
      errors.add(:identifier, "has already been taken")
    end
    #errors.add(:identifier, "has already been taken") if Institution.where(desc_metadata__identifier_ssim: self.identifier).reject{|r| r.identifier == self.identifier}.any?
  end

end

正如您所看到的,我必须编写一种非常不同的方法来检查标识符的唯一性,因为.where方法没有返回任何内容。我没有意识到这是问题,直到我开始处理控制器中的show模型(下面):

def show
  identifier = params[:identifier] << "." << params[:format]
  @institution = Institution.where(desc_metadata__identifier_ssim: identifier)
end

即使我的数据库中有多个Institution对象,并且已经检查过URL参数是否正确,但它仍然没有返回任何内容。并且是在控制台中搜索对象的双重检查的一部分。这是输出:

Loading development environment (Rails 4.0.3)
2.0.0-p353 :001 > ap = Institution.where(desc_metadata__name_ssim: "APTrust")
ActiveFedora: loading fedora config from /Users/kec6en/HydraApp/fluctus/config/fedora.yml
ActiveFedora: loading solr config from /Users/kec6en/HydraApp/fluctus/config/solr.yml
Loaded datastream list for aptrust-dev:379 (3.2ms)
Loaded datastream profile aptrust-dev:379/RELS-EXT (2.7ms)
Loaded datastream content aptrust-dev:379/RELS-EXT (2.4ms)
Loaded datastream profile aptrust-dev:379/descMetadata (2.6ms)
Loaded datastream profile aptrust-dev:379/descMetadata (3.5ms)
Loaded datastream content aptrust-dev:379/descMetadata (3.1ms)
 => [#<Institution pid: "aptrust-dev:379", name: "APTrust", brief_name: "apt", identifier: "aptrust.org">] 
2.0.0-p353 :002 > apt = Institution.where(desc_metadata__identifier_ssim: "aptrust.org")
 => [] 
2.0.0-p353 :003 >

正如您所看到的,我正在查询确实存在的标识符,但它没有找到任何内容。作为参考,这里是我正在处理的元数据数据流。请注意,标识符的索引为:stored_searchable,因此我应该可以查询它。

class InstitutionMetadata < ActiveFedora::RdfxmlRDFDatastream
  map_predicates do |map|
    map.name(in: RDF::DC, to: 'title') { |index| index.as :symbol, :stored_searchable }
    map.brief_name(in: RDF::DC, to: 'alternative')
    map.identifier(in: RDF::DC, to: 'identifier') { |index| index.as :symbol, :stored_searchable }
  end
end

我在name属性之后对其进行了建模,因为它似乎正在运行。知识分子为什么没有想法?

谢谢!

2 个答案:

答案 0 :(得分:0)

为什么不直接搜索标识符而不是使用desc_metadata

Institution.where identifier: "aptrust.org"

答案 1 :(得分:0)

使用desc_metadata__identifier_tesim代替desc_metadata__identifier_ssim似乎可以用于查找对象,尽管它仍然不适用于我编写的单一检查方法。