简体   繁体   中英

Create new ruby model instance with associations

This is perhaps a basic question, but it's currently driving me nuts...Perhaps i'm missing something, since i'm diving so deep in the code, but the question is:

How can I make my object with associations?

I have the following migration scripts:

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

The classes for these objects look like the following:

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

So if I remember my Ruby lessons correctly, the following code should be perfectly fine:

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

So i've written the following line of code. The internals are a bit dark, but I'm 100% sure the hashes generated from the JSON data are correct. Checked this with the debugger:

  # 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

The problem I'm having now is that my PrefLabels are: 1) not beeing created all the time 2) are never linked to my concept objects.

What am I missing?

First, I would suggest simplifying this snippet of your code a bit like so:

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'])

That's untested so it might not be quite right.

Second, check to see if validation is passing or not. valid? will return false if there is a validation issue of some sort -- for example, if the PrefLabel value isn't unique.

I suspect you need to scope your validations, although that's just a guess of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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