Skip to main content
Clarified `it`
Source Link

On a technical note create! will always return a valid object (or raise an exception)

What I usually do is put it in a separate method and use an early return:

user = User.first
return unless user
campaign = user.campaigns.create!(args)
return unless campaign
new_player = game_instance.players.create!(args)
return unless new_player
new_country = new_player.countries.create!(args)
return unless new_county
new_country.states.create!(args) 

You can combine the return as follows:

user = User.first                                 || return
campaign = user.campaigns.create!(args)           || return 
new_player = game_instance.players.create!(args)  || return
new_country = new_player.countries.create!(args)  || return
new_country.states.create!(args)

Or even

campaign = User.first &. campaigns.create!(args)
return unless campaign

game_instance.players.create!(args)  &.
      countries.create!(args)  &.
      states.create!(args)

But I prefer not to use this programming style

-- Update 1

To clarify by it I mean these lines of code. Depending on what you do following this you might want to not put all the code in the same place.

On a technical note create! will always return a valid object (or raise an exception)

What I usually do is put it in a separate method and use an early return:

user = User.first
return unless user
campaign = user.campaigns.create!(args)
return unless campaign
new_player = game_instance.players.create!(args)
return unless new_player
new_country = new_player.countries.create!(args)
return unless new_county
new_country.states.create!(args) 

You can combine the return as follows:

user = User.first                                 || return
campaign = user.campaigns.create!(args)           || return 
new_player = game_instance.players.create!(args)  || return
new_country = new_player.countries.create!(args)  || return
new_country.states.create!(args)

Or even

campaign = User.first &. campaigns.create!(args)
return unless campaign

game_instance.players.create!(args)  &.
      countries.create!(args)  &.
      states.create!(args)

But I prefer not to use this programming style

On a technical note create! will always return a valid object (or raise an exception)

What I usually do is put it in a separate method and use an early return:

user = User.first
return unless user
campaign = user.campaigns.create!(args)
return unless campaign
new_player = game_instance.players.create!(args)
return unless new_player
new_country = new_player.countries.create!(args)
return unless new_county
new_country.states.create!(args) 

You can combine the return as follows:

user = User.first                                 || return
campaign = user.campaigns.create!(args)           || return 
new_player = game_instance.players.create!(args)  || return
new_country = new_player.countries.create!(args)  || return
new_country.states.create!(args)

Or even

campaign = User.first &. campaigns.create!(args)
return unless campaign

game_instance.players.create!(args)  &.
      countries.create!(args)  &.
      states.create!(args)

But I prefer not to use this programming style

-- Update 1

To clarify by it I mean these lines of code. Depending on what you do following this you might want to not put all the code in the same place.

Source Link

On a technical note create! will always return a valid object (or raise an exception)

What I usually do is put it in a separate method and use an early return:

user = User.first
return unless user
campaign = user.campaigns.create!(args)
return unless campaign
new_player = game_instance.players.create!(args)
return unless new_player
new_country = new_player.countries.create!(args)
return unless new_county
new_country.states.create!(args) 

You can combine the return as follows:

user = User.first                                 || return
campaign = user.campaigns.create!(args)           || return 
new_player = game_instance.players.create!(args)  || return
new_country = new_player.countries.create!(args)  || return
new_country.states.create!(args)

Or even

campaign = User.first &. campaigns.create!(args)
return unless campaign

game_instance.players.create!(args)  &.
      countries.create!(args)  &.
      states.create!(args)

But I prefer not to use this programming style