SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Jan 20, 2006, 10:34 #1
- 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 ..
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
-
Jan 20, 2006, 10:43 #2
Have you tried using @session instead of session?
-
Jan 20, 2006, 12:06 #3
- 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.[]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
-
Jan 20, 2006, 12:12 #4
- 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.
-
Jan 20, 2006, 12:16 #5
- 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
-
Jan 20, 2006, 12:25 #6
- 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
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Jan 20, 2006, 14:40 #7
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.
-
Jan 22, 2006, 19:09 #8
- 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