views:

41

answers:

1

I'm using the facebooker gem which creates a variable called facebook_session in the controller scope (meaning when I can call facebook_session.user.name from the userscontroller section its okay). However when I'm rewriting the full_name function (located in my model) i can't access the facebook_session variable.

+1  A: 

You'll have to pass the value into your model at some point, then store it if you need to access it regularly.
Models aren't allowed to pull data from controllers -- it would break things in console view, unit testing and in a few other situations.

The simplest answer is something like this:

class User
    attr_accessor :facebook_name
    before_create :update_full_name

    def calculated_full_name
       facebook_name || "not sure"
    end

    def update_full_name
       full_name ||= calculated_full_name
    end
end


class UsersController
    def create
       @user = User.new params[:user]
       @user.facebook_name = facebook_session.user.name

       @user.save
    end
end
austinfromboston
Thanks for your response Austin, in this situation would this code work if you had something like this:x = User.find(1)x.full_nameI'm not sure if the create method is ever called like this.Thanks
Brian
I've clarified my code so you know how to save the facebook_name to the database. Now it should work to retrieve it as you suggest.
austinfromboston