Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top