Forum: Ruby on Rails Newbie question about records

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mikerthomsen (Guest)
on 2005-11-29 01:56
(Received via mailing list)
I have a controller named Login, a model named Login and a table named
logins (why did Rails insist that this had to be logins, instead of
login?) and the controller looks like this:

class LoginController < ApplicationController
	scaffold :login

	def index()
	end

	def verify()
		formVars = @params["login"]
		if formVars == nil
			render_text("I hate to break it to you, but you really do have to log
in.")
		end
	end
end

This is the SQL for the logins table:

create table logins
(
	id int not null primary key,
	username varchar(64) unique not null,
	password varchar(64) not null,
	email varchar(96) default "",
	real_name varchar (64) default ""
);

Now what I can't figure out right now is how do I connect to the
database the "rails way," and check to see if formVars["username"] and
formVars["password"] are valid according to the entry in the database?

Also, would anyone recommend the book Agile Web Development with Ruby
on Rails as a comprehensive guide to all things Rails?

Mike
739e2894aaaa2e2da1627dd5d819fba7?d=identicon&s=25 Daniel -. (liquid)
on 2005-11-29 02:28
(Received via mailing list)
Hi Micheal,

On 11/29/05, Michael Thomsen <mikerthomsen@gmail.com> wrote:
>
> I have a controller named Login, a model named Login and a table named
> logins (why did Rails insist that this had to be logins, instead of
> login?) and the controller looks like this:


Plural table names are a convention in Rails.  Rails is convention based
and
if you follow the convention it makes life easier.  Not that you have to
though.  Look at the docs if you want to call your table / primary key
something different.


class LoginController < ApplicationController
>                 end
>         email varchar(96) default "",
>         real_name varchar (64) default ""
> );
>
> Now what I can't figure out right now is how do I connect to the
> database the "rails way," and check to see if formVars["username"] and
> formVars["password"] are valid according to the entry in the database?


In general, in your view, you would call the text fields like

<%= text_field 'login', 'username' %>
<%= password_field 'login', password' %>

This makes @params[:login]['username'] and @params[:login]['password']
available to your controller.

The general format for the text_field is <%= text_field 'object',
'method'
%>  If you follow this convention rails will automagically fill out the
form
if it's not filled out correctly.  It will also display and highlight
errors
with the scaffolding.

To confirm that the password / username is valid, you could use in your
controller
@login = Login.new(@params[:user])
if @login.valid?
or
if @login.save

These will tell you if your model is valid acording to your validate_xxx
statements in your model.

ie.

class Login < ActiveRecord::Base
 validate_presence_of :username, :password
 validate_uniquness_of :username
validate_format_of :email, some regex
etc etc
end

There is pretty good docs on rails wrt this, and there is also plenty of
discussion on this list.

Also, would anyone recommend the book Agile Web Development with Ruby
> on Rails as a comprehensive guide to all things Rails?


Definitely I would recommend this book.  But you should also supplement
this
with some research into Ruby.
http://documentation.rubyonrails.com/

Whys poignant guide <http://poignantguide.net/ruby/> - This is a strange
but
informative book

http://www.ruby-lang.org/en/20020103.html
Gives a good list of ruby related docs to start with.

Good luck

Mike
mikerthomsen (Guest)
on 2005-11-29 02:40
(Received via mailing list)
Thanks, I appreciate it. Any recommendations on a good Ruby book? I've
printed out most of the Programming Ruby book listed on ruby-lang.org,
but I was wondering if there was something considered even better.
mikerthomsen (Guest)
on 2005-11-29 02:44
(Received via mailing list)
Also, I am a bit confused by the :user statements. Ruby seems overall
very straight forward to me since I have experience with Python (not
too alien at all), but I was wondering what the :user and similar
statements mean.
sitharus (Guest)
on 2005-11-29 03:08
(Received via mailing list)
On 29/11/2005, at 2:41 PM, Michael Thomsen wrote:

> Also, I am a bit confused by the :user statements. Ruby seems overall
> very straight forward to me since I have experience with Python (not
> too alien at all), but I was wondering what the :user and similar
> statements mean.

:user is a symbol. A symbol is also known as an interned string, it's
basically an immutable string much like Python's strings. Symbols are
also only created once and shared through the whole application, so
are faster than strings if you just want a label rather than a full
blown string object.

Also Ruby allows you to omit parentheses where there's no ambiguity,
so has_one :user can also be written has_one(:user).
739e2894aaaa2e2da1627dd5d819fba7?d=identicon&s=25 Daniel -. (liquid)
on 2005-11-29 03:45
(Received via mailing list)
Sorri theres a typo...

@login = Login.new(@params[:user])

Should be

@login = Login.new(@params[:login])
739e2894aaaa2e2da1627dd5d819fba7?d=identicon&s=25 Daniel -. (liquid)
on 2005-11-29 03:49
(Received via mailing list)
Besides the rubydoc page, there are another good ones from The Pragmatic
Programmers <http://www.pragmaticprogrammer.com/index.html>.
13df438de19ca028b7aaf2bf5658dcdf?d=identicon&s=25 Jeff C. (jeff)
on 2005-11-29 05:06
liquid wrote:
> Sorri theres a typo...
>
> @login = Login.new(@params[:user])
>
> Should be
>
> @login = Login.new(@params[:login])

I'm wondering why it's ok to call Login.new with the @params hash, when
he hasn't defined Login#initialize to accept a hash parameter?

Or is that already defined for him by the base class somehow?
mikerthomsen (Guest)
on 2005-11-29 06:42
(Received via mailing list)
I suppose I was a little bit vague with my db question. I have a login
page and new to validate the username against the database. There are
5 fields in the database, but only two that are checked during the
verify action. I'm coming from a J2EE background so I guess when I see
ActiveRecord, I think JavaBean.

I try this for example:

@row = {"username"=>"testuser"}
@login = Login.new(@row)

but it won't run because I haven't specified an ID value. How would I
go about finding the ID here? Also, how do I get the next primary id
from the database for a new record?
sitharus (Guest)
on 2005-11-29 06:46
(Received via mailing list)
On 29/11/2005, at 6:40 PM, Michael Thomsen wrote:

>
> but it won't run because I haven't specified an ID value. How would I
> go about finding the ID here? Also, how do I get the next primary id
> from the database for a new record?

new always creates a brand new object, it will never ever search for
one. You want find.

Login.find(:first, :conditions=>["username=?","testuser"])
or
Login.find_by_username("testuser")

As for the next ID, let Rails handle it.
mikerthomsen (Guest)
on 2005-11-29 08:43
(Received via mailing list)
Thanks for the help! So, the only question I think I have left for now
is, is find_by_username something that's automagically created by the
ActiveRecord system for my particular Login model? So if I had a field
in the table named "disk_space_used" would it generate one named
"find_by_disk_space_used?"

Thanks again for your help.
mikerthomsen (Guest)
on 2005-11-29 08:43
(Received via mailing list)
Oh yeah, what I meant by needing to find the next ID is that if I want
to create a new model, how should I feed it a new ID field? I'm using
AUTO_INCREMENT with my ID field but that's only good for MySQL and
eventually I want to use SQLite for the app I am hoping to develop
with RoR. What I was wondering is if there is a way to create that new
Login object and give it the appropriate ID without falling back on
AUTO_INCREMENT.
sitharus (Guest)
on 2005-11-29 08:52
(Received via mailing list)
On 29/11/2005, at 8:12 PM, Michael Thomsen wrote:

> Thanks for the help! So, the only question I think I have left for now
> is, is find_by_username something that's automagically created by the
> ActiveRecord system for my particular Login model? So if I had a field
> in the table named "disk_space_used" would it generate one named
> "find_by_disk_space_used?"

Yes, they're created dynamically. You can even get
find_by_disk_space_used_and_username and so forth. I can't recall
where this is specified in the documentation though.

> Oh yeah, what I meant by needing to find the next ID is that if I want
> to create a new model, how should I feed it a new ID field? I'm using
> AUTO_INCREMENT with my ID field but that's only good for MySQL and
> eventually I want to use SQLite for the app I am hoping to develop
> with RoR. What I was wondering is if there is a way to create that new
> Login object and give it the appropriate ID without falling back on
> AUTO_INCREMENT.

You'll either have to roll your own or just rely on the database. At
the moment Rails is designed to use the database to manage it.
grzm (Guest)
on 2005-11-29 09:24
(Received via mailing list)
On Nov 29, 2005, at 16:15 , Michael Thomsen wrote:

> I'm using
> AUTO_INCREMENT with my ID field but that's only good for MySQL and
> eventually I want to use SQLite for the app I am hoping to develop
> with RoR.

SQLite3 supports autoincrement:

http://www.sqlite.org/faq.html#q1

Does this not work with Rails?

Michael Glaesemann
grzm myrealbox com
09d8790f3ca9ee66f56b4262c106ad3f?d=identicon&s=25 Jarkko L. (jarkko)
on 2005-11-29 09:44
(Received via mailing list)
On 29.11.2005, at 9.51, Michael Glaesemann wrote:

> http://www.sqlite.org/faq.html#q1
>
> Does this not work with Rails?

Since SQLite is one of the supported databases and Rails relies on
the autoincrement features of db's, I've a hard time seeing why it
wouldn't work. The bottom line is that in most cases when creating a
new object, you don't have to worry about the id at all.

//jarkko
mikerthomsen (Guest)
on 2005-11-29 16:13
(Received via mailing list)
Thanks, guys. I feel stupid. All I had to do was add "id"=>nil to the
hashtable passed to my model and it worked out just fine. Thanks for
the help!
This topic is locked and can not be replied to.