Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Rails 4. Update User when creating different model.

Just to show you how I got to this point:

Every user has many profiles. Every profile has type recognized by single table inheritance(amateur, professional, and some other). I need to store current_profile somewhere and somehow.

Professionals Controller

class ProfessionalsController < ApplicationController
def create
  @professional = Professional.new(professional_params)
  @user = current_user
  @professional.user_id = current_user.id
  @update_current_profile = User.update(@user, {:current_profile => @professional.id})
  if @professional.save

  else

  end
end

private

  def professional_params
    params.require(:professional).permit(:id, :username, :user_id)
  end

end

This is meant to update current_profile of user to the newly created professional profile and do some staff then.

When the profile is created current_profile is set(updated) to NULL. If I change :

@update_current_profile = User.update(@user, {:current_profile => @professional.id})

to something different, for example:

@update_current_profile = User.update(@user, {:current_profile => @professional.user_id})

or

@update_current_profile = User.update(@user, {:current_profile => 3})

it stores data in User.current_profile perfectly.

I was trying @professional without an .id too. Why is this doing so?

Another question is. Is this the best way to store current_profile of user? Would you recommend me any better/safer/more efficient solution?

Thanks all of you guys.