SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session data available in controller, but not model?

    Ok, thanks to everyone who's helped me out so far... so close to being done with the boring parts

    I have documents that upload, and I need to associate a id that's in a session to the document. What I have in my model is:

    Code:
    class Document < ActiveRecord::Base
      ..
    
      def before_save
        self.project_id = session[:project_id]
        ..
      end
    ..
    Basically, it's saying it's not available (undefined local variable or method `session' for #<Object:0x2799258>)

    However, it's available in the controller. Is there something I should be doing to get it there? Maybe im just not sure why session is local to the controller and not the model...
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Have you tried using @session instead of session?

  3. #3
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vinnie,

    It doesnt, I get:

    ou have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occured while evaluating nil.[]
    Thanks for your help
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am certainly not a Rails or Ruby expert, but since you are probably accessing the model either in the context of a method of the controller, or in a view eventually routed to from the controller, couldn't you just have the controller pass the session into a model which needed access to it?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jason, I am actually not sure. I know that a method called "before_save" in the model will be called before your data is saved, so I have this to deal with a file upload, and the project id

    Code:
    def before_save
        # set the project id
        self.project_id = @session[:project_id]
    
        # handle the file upload
        file_name = Time.now.to_i
        
        file = self.file_name.rewind
    
        File.open("in_docs/#{file_name}", 'wb') { |f| f.write(file.read) }
    
        self.file_name      = file.original_filename
        self.file_real_name = file_name
        self.content_type   = file.content_type
      end
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe just remove the session dependancy from the Model entirely?

    Code:
    # in the controller
      def file_upload
        file = new ModelThing
        file.project_id = session[:project_id]
        file.save
      end
    So that all of the session manipulation is external to the Model code?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #7
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The session isn't available in the model because it shouldn't be, to maintain the separation of business and application logic that MVC provides. You should pass any values needed by your model into it as a method parameter (or possibly in the constructor depending on where you need that value), regardless of where that value comes from.

    EDIT - or do what Jason suggested.

  8. #8
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ditto to what Luke says. Again though I have found that occasionally that I get annoyed because I can not do everything in the model I want to do, but there is a good reason for it.

    If you need access to things like sessions, params and url manuipulation etc in your model, there it saying there's smething wrong about your design.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •