# Noob struggling to learn

**URL:** <https://www.ruby-forum.com/t/noob-struggling-to-learn/91218>\
**Category:** Rails\
**Created:** [April 5, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218 "2007-04-05T19:25:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 5, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/1 "2007-04-05T19:25:16Z")

</div>

I am learning Rails by building a real application for someone.  
Ultimately the app may become a much larger thing, however for now i  
am concentrating on just a tricky login situation. I will essentially  
have 2 pages, a ‘start’ and a ‘login’ page. ‘start’ being the initial  
screen for the application. What I need this start page to do is pull  
the UserID from the URL (assuming the request has the userID) and then  
use that UserID to look in a table for another bit of information  
(planID type of thing) and store that ‘planID’ in a session to be  
brought forward to the login and beyond. Essentially the login can be  
very different depending on that planID, this is the reason for this  
work on the start page. The start page may also contain some nice  
graphics and a disclaimer or something. I used RAKE MIGRATE to build  
my table, I created my model for that table and I created a controller  
called ‘start’. Everything seems hooked up properly and I can run  
it. I created the ‘start’ controller assuming this logic of grabbing  
the ID from the URL, looking in the table (SQL?), and storing the  
result ID in the the session would go in the controller? If anyone  
could take the time to help me over this block, I would appreciate it  
very much. I am not even sure what the ruby code would look like for  
searing the table, or the ruby code for grabbing the ID from the URL  
or storing the information in the session.

advanced thank you  
JR

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 5, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/2 "2007-04-05T21:16:03Z")

</div>

I recommend the book: “Agile Web D. with Rails”.

Check it out at [Amazon.com](http://Amazon.com):  
[http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fdp%2F0977616630%2F&tag=gametroll-20&linkCode=ur2&camp=1789&creative=9325](http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fdp%2F0977616630%2F&tag=gametroll-20&linkCode=ur2&camp=1789&creative=9325)

I found that the book is very much better than the available web  
resources. I think after you read the book you’ll find what you want to  
do pretty easy with rails.

If you want to store stuff in the session then you do something like (in  
your controller):

session[:plan\_id] = params[‘plan\_id’]

It wasn’t clear to me what you mean by pulling the user\_id from the URL,  
particularly since that is a start screen, I presume the user hasn’t  
logged in yet.

The rails book has a whole section on how to do login, authentication  
and such. You probably want to use filters as well.

regards,

Richard.

removed\_email\_address@domain.invalid wrote:

> I am learning Rails by building a real application for someone.  
> Ultimately the app may become a much larger thing, however for now i  
> am concentrating on just a tricky login situation. I will essentially  
> have 2 pages, a ‘start’ and a ‘login’ page. ‘start’ being the initial  
> screen for the application. What I need this start page to do is pull  
> the UserID from the URL (assuming the request has the userID) and then  
> use that UserID to look in a table for another bit of information  
> (planID type of thing) and store that ‘planID’ in a session to be  
> brought forward to the login and beyond. Essentially the login can be  
> very different depending on that planID, this is the reason for this  
> work on the start page. The start page may also contain some nice  
> graphics and a disclaimer or something. I used RAKE MIGRATE to build  
> my table, I created my model for that table and I created a controller  
> called ‘start’. Everything seems hooked up properly and I can run  
> it. I created the ‘start’ controller assuming this logic of grabbing  
> the ID from the URL, looking in the table (SQL?), and storing the  
> result ID in the the session would go in the controller? If anyone  
> could take the time to help me over this block, I would appreciate it  
> very much. I am not even sure what the ruby code would look like for  
> searing the table, or the ruby code for grabbing the ID from the URL  
> or storing the information in the session.
> 
> advanced thank you  
> JR

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/3 "2007-04-12T15:47:21Z")

</div>

Thanks to both of you.  
JR

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 7, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/4 "2007-04-07T17:42:58Z")

</div>

Jason:

I also recommend it. If you need it right away, you can order a pdf  
from [http://pragmaticprogrammer.com/titles/rails/index.html](http://pragmaticprogrammer.com/titles/rails/index.html).

I also attended Dave T.’ and Mike C.'s Pragmatic Studio class  
which was excellent.

Here are some tips:

1. Lookup routes to see how to get the id from a URL. Your url would  
be like [http://example.com/controller/action/:id](http://example.com/controller/action/:id) instead of http://  
[example.com/controller/action?id=blah](http://example.com/controller/action?id=blah). You could then access the id  
from params[:id] in your controller.
2. To find an object, you don’t have to write SQL right away. Lookup  
dynamic finders to see how User.find\_by\_id(:id) will return your user  
object. Or, use User.find() if you want more options.

I agree with Richard that you might want to see the book’s section on  
logins. It will save you lots of effort. I know, because I wrote my  
own login stuff and later found out how easy if could have been.

Keep asking questions and good luck,

-Anthony

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 4:46am UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/5 "2007-04-18T04:46:55Z")

</div>

ok here is my first pass…

#parse out the userID from th url  
#query DB for planID using the userID  
#store planID to session to be used going forward

def url  
@planid = userid.find(@params[:id])  
end

do you think this method does what I am attempting to do based on my  
commented lines?

JR

On Apr 12, 9:45 am, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/6 "2007-04-18T17:22:23Z")

</div>

yes, VERY helpful. But I actualy mistated my intentions slightly.  
What I am trying to do is:

#parse out the userID from th url  
#query the users table for clientID (using the userID)  
#query the plans table (using the newly aquired clientID) for planID  
#store planID to session to be used going forward

sorry for the confusion. Your help is appreciated. I would assume  
the solution goes in the controller as a method. Most likely in the  
users controller hooked up the users table?

thanks again

JR

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/7 "2007-04-18T12:12:29Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ok here is my first pass…
> 
> #parse out the userID from th url  
> #query DB for planID using the userID  
> #store planID to session to be used going forward
> 
> def url  
> @planid = userid.find(@params[:id])  
> end
> 
> do you think this method does what I am attempting to do based on my  
> commented lines?
> 
> JR
> 
> On Apr 12, 9:45 am, “removed\_email\_address@domain.invalid”

Does the plans table have a user\_id, or does the users table have a  
plan\_id ?

If the former,

session[:plan\_id] = Plan.find\_by\_user\_id(params[:id]).id

If the latter,

session[:plan\_id] = User.find(params[:id]).plan.id

In either case, you’re getting a Plan object from the plans table,  
either by finding a user, then getting ‘his’ plan, or by looking up a  
Plan directly by the user\_id column.

Does this help at all ?

Alan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 19, 2007, 11:07am UTC](https://www.ruby-forum.com/t/noob-struggling-to-learn/91218/8 "2007-04-19T11:07:44Z")

</div>

removed\_email\_address@domain.invalid wrote:

> yes, VERY helpful. But I actualy mistated my intentions slightly.  
> What I am trying to do is:

Excellent.

> #parse out the userID from th url  
> #query the users table for clientID (using the userID)  
> #query the plans table (using the newly aquired clientID) for planID  
> #store planID to session to be used going forward

Thats fine. It’s Rinse…repeat.

As long as you have the association set up (has\_many, belongs\_to etc)  
you can just do:

session[:plan\_id] = Users.find(params[:id]).client.plan.id

i.e  
User belongs\_to Client / Client has\_many Users  
Client has\_one Plan / Plan belongs\_to Client

> sorry for the confusion. Your help is appreciated. I would assume  
> the solution goes in the controller as a method. Most likely in the  
> users controller hooked up the users table?

Well, wherever you have implimented the action you’re invoking.

Say you’re hitting /users/show\_plan?id=26

you’ll need, in UsersController

def show\_plan

# get the @plan so we can show it in show\_plan.rhtml

@plan = Users.find(params[:id]).client.plan

# store the id in the session for later.

session[:plan\_id] = @plan.id  
end

Does this help ? This whole actions and controller thing is pretty  
fundamental to rails, so if it’s not immediately clear what the above  
para means, I’d _strongly_ urge to get and read the AWDwR book from the  
Pragmatic Programmers. (but feel free to keep asking questions 🙂

Alan
