문제

such check will be right on the created object?

I have a feeling that this isn't really correct.

  unless DailyTip.find_by_slug(tip[:slug]).persisted?
    daily_tip = DailyTip.create!(
      tip: tip[:tip],
      ...
    )

    day = ProgramDay.find_by_day(tip[:program_day])
    day.update_attributes(daily_tip_id: daily_tip.id)
  else
    puts 'Video tip already created.'
  end
도움이 되었습니까?

해결책

this line

DailyTip.find_by_slug(tip[:slug]).persisted?

does not make much sense because find_by_slug will query the db and if something is found it will check whether it is already saved to the db. If nothing is found your code will break with:

 undefined method `persisted?' for nil:NilClass

I think you meant to write something like this:;

if DailyTip.find_by(slug: tip[:slug])
  puts "Video tip already created"
else
  daily_tip = DailyTip.create! tip: tip[:tip]
  day = ProgramDay.find_by_day(tip[:program_day])
  day.update_attributes(daily_tip_id: daily_tip.id)
end

Finally, a lot depends on your app logic. I hope this helps a bit.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top