Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Sessions in models - that thing you're not supposed to do....


  • Please log in to reply
4 replies to this topic

#1 Kelli Shaver

Kelli Shaver

    Inspector

  • Administrators
  • 75 posts
  • LocationKentucky

Posted 11 September 2013 - 02:48 PM

I've been looking for a quick/easy way to access session variables inside of a model and I can't find a good, simple one.

 

I've found dozens of posts saying this is "very bad" and a "fundamental design flaw" of my application, all the way to much more colorful expressions of exactly how wrong this is. The reasoning I keep seeing is that models are a layer to the database, and therefore shouldn't have anything to do with a user session.

 

I get that, but I think it's pretty short-sighted.

 

You see, my app has no database - It pulls all of its data from an external API.

 

Aside from this fact, it needs to function the same way any other Rails application would, and it makes sense to have all of those records I'm pulling from the remote API handled via models. I still want to be able to use ActiveModel validators to validate data before I send it to the API, still want to keep controller code clean, etc.

 

But in order to talk to the API, I need to pass an authentication token, which I'm storing in the session.

 

Am I just way wrong in my thinking here? I don't think I am.

 

Also, anyone have a quick solution? Passing the session to the model method won't work, because it's a private method that's called by almost every other method in the model (to set the hTTP headers for my request).

 



#2 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 11 September 2013 - 05:10 PM

 The reasoning I keep seeing is that models are a layer to the database, and therefore shouldn't have anything to do with a user session.

 

 

I've watched a presentation from (can't remember who) about the origins of the MVC design pattern.  It's before the Internet era where on your GUI screen, each button, element, etc on the user's screen... is a View, and supporting each View is it's own controller and they all connect to the business model, (take note: I didn't say database). 

 

But over time, that MVC pattern became bastardized (like in Rails) and now the whole big screen you see is the View, all elements on the screen/webpage/whatever is controlled by a single Controller, and the Model is the database. 

 

What I learned from the video is the "M" in Model is the Business Model.... not necessarily the Database.  The Database is just persistent storage.  You can have a business model (or business logic) without a database (if the business logic doesn't need persistent storage).  The database, or datasource, may not even be a real database server... it could just be a set of flat files stored on the file system. It satisfies the need for a persistent data storage, without it being a database. 

 

You see, my app has no database - It pulls all of its data from an external API.

 

 

So in your case, you treat the external API as your datasource.... like a remote database. I don't see anything wrong with it. But I'm just a Rails newb, what do I know. 

 

For that matter, the datasource can be a data structure in memory, but of course that's not persistent. 

 

So really, I see the Database as coming into play (or necessity), only when the business logic (or business Model) requires persistent data storage. The "M" should really be about the business model.  

 

 

 

... anyways, I'm just blabbing here. I don't even know if this contribute anything. I don't have a solution to your sessions question :)



#3 Kelli Shaver

Kelli Shaver

    Inspector

  • Administrators
  • 75 posts
  • LocationKentucky

Posted 11 September 2013 - 05:44 PM

What I learned from the video is the "M" in Model is the Business Model.... not necessarily the Database.  The Database is just persistent storage.  You can have a business model (or business logic) without a database (if the business logic doesn't need persistent storage).  The database, or datasource, may not even be a real database server... it could just be a set of flat files stored on the file system. It satisfies the need for a persistent data storage, without it being a database. 

 

So in your case, you treat the external API as your datasource.... like a remote database. I don't see anything wrong with it. But I'm just a Rails newb, what do I know. 

...

 

So really, I see the Database as coming into play (or necessity), only when the business logic (or business Model) requires persistent data storage. The "M" should really be about the business model.  

 

... anyways, I'm just blabbing here. I don't even know if this contribute anything. I don't have a solution to your sessions question :)

 

Yes! Thank you. You're right, it doesn't solve my problem, but it's nice to have a little validation for my thinking. ;)

 

Model != database table *sigh*



#4 katafrakt

katafrakt

    Signalman

  • Members
  • 16 posts
  • LocationKrak√≥w, PL

Posted 12 September 2013 - 06:01 AM   Best Answer

But the token still should be called by the controller! Can't you instead of

@my_model = MyModel.find(1234)

just do something like

@my_model = MyModel.get_from_api(session[:token], {id: 1234})

?


  • Kelli Shaver likes this

#5 Kelli Shaver

Kelli Shaver

    Inspector

  • Administrators
  • 75 posts
  • LocationKentucky

Posted 17 September 2013 - 07:45 PM

I think that's what I'll end up doing, yes. It just seems.... inefficient to have to pass the API token to every single method that acts on said API. 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users