small medium large xlarge

Back to: All Forums  Core Data
28 Jan 2010, 10:04
Michael Johnston (9 posts)

With the custom migration in the book, and with the sample code for v3, RecipeIngredients get their proper relationship to Unit of Measure, but Ingredients do not, and so end up with unit of measure set to no value. I can’t figure out how to make sure that when the unitOfMeasure attribute on RecipeIngredient is turned into a relationship, that the corresponding UnitOfMeasure is related to the corresponding Ingredient.

31 Jan 2010, 22:23
Michael Johnston (9 posts)

I did manage to figure this out and it was actually pretty simple. I did it in the createRelationshipsForDestinationInstance method of RecipeIngredientToIngredient. Here it is (but in MacRuby syntax)

  def createRelationshipsForDestinationInstance(dInstance, entityMapping:mapping, manager:manager, error:error)
    # get the source instance
    sources = manager.sourceInstancesForEntityMappingNamed("RecipeIngredientToIngredient", destinationInstances:[dInstance])
    if sources.nil? || sources.empty?
      error_info = {NSLocalizedDescriptionKey => "No source entity found for #{dInstance.inspect} (name: #{dInstance.valueForKey("name")})"}
      error[0] = NSError.errorWithDomain("lastobelus", code:8001, userInfo:error_info)
      return false
    source = sources.first
    uom = source.valueForKey("unitOfMeasure")
    uom_entity = manager.userInfo[:unitsOfMeasure][uom]
    if uom_entity.nil?
      error_info = {NSLocalizedDescriptionKey => "No unit of measure entity found for #{source.inspect} (name: #{uom})"}
      error[0] = NSError.errorWithDomain("lastobelus", code:8002, userInfo:error_info)
      return false
    dInstance.setValue( uom_entity, forKey:"unitOfMeasure")
You must be logged in to comment