Importing CSV data into Rails app, using something other then the association “id”

前端 未结 3 1777
我寻月下人不归
我寻月下人不归 2021-01-06 01:18

I am trying to import a few CSV files into my rails app. I learned and managed to import tables into Models without association.

Now i have managed to import the da

3条回答
  •  爱一瞬间的悲伤
    2021-01-06 01:36

    A shipment_type is a ruby object, you want to send a string.

    If you are needing to import relationships, add methods on the Port model like so

    class Port < ApplicationRecord
    
      def shipment_type_name
        shipment_type.try(:name)
      end
    
      def shipment_type_name=(name)
        self.shipment_type = ShipmentType.where(:name => name).first_or_create
      end
    
      def country_country_code
        country.try(:country_code)
      end
    
      def country_country_code=(code)
        self.country = Country.where(:country_code => code).first
      end
    
    
    end
    

    Then in the CSV you'd send a shipment_type_name and country_country_code attributes.

    You would do something similar to other relationships.

提交回复
热议问题