-1

UPDATE:

Thanks guys, went with this:

def add_minor
  if @user.minors.count <= 2
    render :json =>  @user.profile.minors << Minor.find(params[:minor_id]), :status => 200
  else
    render :json => '', :status => 409
  end
end 

is there a better way to handle the assignment of this res property here to keep it available to render call? Overall I am just wondering how to improve this pretty smelly method.

respond_to :json
def add_minor
  res = ''
  unless @user.minors.count > 2
    minor = Minor.find(params[:minor_id])
    res =  @user.profile.minors << minor
    status = 200
  else
    status = 409
  end
  render :json => res, :status => status
end
errata
  • 23,596
  • 2
  • 22
  • 32
  • 1
    This is going to be a matter of opinion, but I wouldn't use variables in this simple case for `res` and `status`. And I would reverse the logic and use `if` (`unless-else` makes my brain hurt). So have `if @user.minors.count <= 2` followed by `render :json => @user.profile.minors << minor, :status => 200` and the `else` be just `render :json => '', :status => 409`. I think the whole thing will be cleaner and easier to read. – lurker Feb 28 '14 at 21:59

1 Answers1

1
def add_minor
  if @user.minors.count > 2
    render :json => '', :status => 409
  else
    @user.profile.minors << Minor.find(params[:minor_id])
    render :json => @user.profile.minors, :status => 200
  end
end
usha
  • 28,973
  • 5
  • 72
  • 93