使用关联创建新的ruby模型实例

时间:2011-05-04 14:03:07

标签: mysql ruby ruby-on-rails-3 associations

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

如何使用关联创建对象?

我有以下迁移脚本:

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)永远不会链接到我的概念对象。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

首先,我建议稍微简化代码片段:

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值不唯一。

我怀疑你需要确定你的验证范围,尽管这只是猜测当然。