ruby on rails - Multiple model.save's in 1 if condition with an unless -
i'm trying save response
, save issue
if it's not nil
in 1 condition don't have multiple if/else conditions complicating logic.
for use case @response
exists , issue
nil, not if block.
is there obvious i'm not seeing or can not write logic in 1 line this?
note: know transaction should used, i'm trying working prototype right now.
if @response.save && (issue.save unless issue.nil?) # not if block when @response exists , issue nil p 'in save' format.html { redirect_to issue_path(params[:issue_id]), notice: success_message } else p 'not in save' format.html { render action: 'new' } end
this have working , hoping there easier 1 liner rather this.
success = false if issue.nil? if @response.save success = true end else if @response.save && issue.save success = true end end if success p 'in save' format.html { redirect_to issue_path(params[:issue_id]), notice: success_message } else p 'not in save' format.html { render action: 'new' } end
you want execute "success" condition when:
@response.save
succeeded andissue
nil
orissue
notnil
, saved successfully.
thus, can do;
if @response.save , (issue.nil? or issue.save) # success else # fail end
Comments
Post a Comment