Вопрос

I have a model in rails called campaign, and another one called export

class Campaign < ActiveRecord::Base
  has_one :export
end

class Export < ActiveRecord::Base
  belongs_to :campaign
end

then I have code:

if campaign.export.nil?
    campaign.create_export(:tag => 'newtag')
end

This code create new line for export again and again because campaign.export is always nil.

Why I created export by campaign.create_export(), but campaign.export is still nil.

Это было полезно?

Решение

My guess here is that your validations for the export are preventing the creation of the export. If you call create_<model> and the validations fail, it will have a copy of the model that was not saved with the validations

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top