我有一个现有的项目记录,我正在导入一个CSV文件来更新相关的项目属性.但是,通常CSV将包含空白字段,如果相关的CSV字段为空,我不想重写现有属性.
像这样的东西:
project.update_attributes(:name => row.field('project_name') unless row.field('project_name').blank?,:owner => row.field('project_owner') unless row.field('project_owner').blank?,:due_date => row.field('project_due_date') unless row.field('project_due_date').blank?)
解决方法
project.update_attributes({:name => row.field('project_name'),:owner => row.field('project_owner'),:due_date => row.field('project_due_date')}.reject{|k,v| v.blank?})