繁体   English   中英

创建具有关联的新 ruby model 实例

[英]Create new ruby model instance with associations

这可能是一个基本问题,但它目前让我发疯......也许我错过了一些东西,因为我在代码中潜水如此深入,但问题是:

如何使我的 object 与关联?

我有以下迁移脚本:

class CreateConcepts < ActiveRecord::Migration
  def self.up
    create_table :concepts do |t|
      t.integer :language_id, :null => false
      t.string  :uri
      t.integer :isco_code
      t.timestamps
    end
  end

  def self.down
    drop_table :concepts
  end
end

class CreatePrefLabels < ActiveRecord::Migration
  def self.up
    create_table :pref_labels do |t|
      t.integer :language_id
      t.integer :concept_id
      t.string :value
      t.timestamps
    end
  end

  def self.down
    drop_table :pref_labels
  end
end

class CreateLanguages < ActiveRecord::Migration
  def self.up
    create_table :languages do |t|
      t.string :code
      t.timestamps
    end
  end

  def self.down
    drop_table :languages
  end
end

这些对象的类如下所示:

class Concept < ActiveRecord::Base
  belongs_to :language
  has_one :pref_label
  validates_uniqueness_of :isco_code
end

class PrefLabel < ActiveRecord::Base
  belongs_to :language
  belongs_to :concept
  validates_uniqueness_of :value
end

class Language < ActiveRecord::Base
  has_many :concepts
  has_many :pref_labels
  validates_uniqueness_of :code
end

因此,如果我正确地记住了我的 Ruby 课程,那么以下代码应该完全没问题:

concept = Concept.first
concept.pref_label
language = Language.find(:code => "en")
language.pref_labels
language.concepts

所以我写了以下代码行。 内部有点暗,但我 100% 确定从 JSON 数据生成的哈希值是正确的。 用调试器检查了这个:

  # This function will retrieve all the top most concepts from the ESCO API in the
  # specified language as an array of Ruby objects, each represented by the relevant class.
  # If the relevant concept does not exist in the database, the entry is automatically created.
  # The function will fall back to English by default if no language is specified
  def self.top_concepts(lang = Language.find_by_code('en') || Language.create(:code => 'en'))
    concepts = []
    json = HTTParty.get "#{Setting[:api]}/getTopmostConcepts?language=#{lang.code}"
    collection = JSON.parse json.parsed_response
    collection.each do |c|
      language = Language.find_by_code c['language'] || Language.create(:code => c['language'])
      concept = Concept.create(:language => language, :uri => c['uri']['uri'], :isco_code => c['iscoCode'].to_i)
      label = PrefLabel.find_by_concept_id(concept.id) || PrefLabel.create(:language_id => language.id, :concept_id => concept.id, :value => c['prefLabel']['string'])
      concept.pref_label= label
      concept.save
      concepts << concept
    end
    return concepts
  end

我现在遇到的问题是我的 PrefLabels 是:1)一直没有被创建 2)从未链接到我的概念对象。

我错过了什么?

首先,我建议简化您的代码片段,如下所示:

language = Language.find_or_create_by_code(c['language'])
concept = langauge.concepts.create(:uri => c['uri']['uri'], :isco_code => c['iscoCode'].to_i)
concept.pref_label = PrefLabel.find_or_create_by_concept_id(:concept_id => concept.id, :language_id => language.id, :value => c['prefLabel']['string'])

这是未经测试的,所以它可能不太正确。

其次,检查验证是否通过。 valid? 如果存在某种验证问题,将返回 false ——例如,如果 PrefLabel 值不是唯一的。

我怀疑您需要 scope 您的验证,尽管这当然只是一个猜测。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM