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