The issue is that the model classes in Rails don't know anything about the controller nor the session. I'm presuming you are using some authentication mechanism that gives you a current_user in your session... so we have to get that into your add_phone method.
In the controller, I'd change this line:
@line_item = @cart.add_phone(phone.id)
to this:
@line_item = @cart.add_phone(phone, current_user)
(notice there are two changes there - one to pass the phone rather than its id, since you already found it, and one to pass the current user.
Then we want to change your add_phone method to look like this:
def add_phone(phone, current_user = nil)
# make sure your Item class initializes quantity to o, not nil.
current_item = line_items.find_or_initialize_by_phone_id(phone.id)
current_item.increment!(:quantity)
current_item.phone.decrement!(:stock)
current_item.user = current_user
current_item.phone.save
current_item
end
notice I'm setting the user to nil by default... that way, code you already have that doesn't provide that field will continue to work. I'm also simplifying your method a little bit with the find_or_initialize_by helper... avoids an 'if' test. Also, the increment and decrement helpers clean up the intent of the code a little.
You should make sure your line item class includes
belongs_to :user
If you find yourself in a situation where you need to know current_user for a lot of domain logic, you might want to check out the sentient_user gem.