Help me understand ruby on rails (beginner)

Hi,

Please forgive the simplicity of the questions to follow.

I’ve written “offline” software in C++/Java/Actionscript 3.0, etc but
have never gotten into web development before. I heard about ruby on
rails and got excited.

I am a completely new to ruby and rails (and web development in
general). I’ve started reading about it as much as I could but I’ve
exhausted all the basic “do this, now do that” tutorials which do a good
job at holding your hand but don’t really explain how things work so
that you can do something different.

I just have a couple of questions that pertain to ruby+rails and
databases in general.

  1. Database 101: When you use the scaffold script to create a site
    (with mySQL as the DB) how does rails create a database + tables? Where
    can you add more columns? Where is the database located? How can you
    view its contents? I’ve never worked with a database before so I’m a
    little confused as to where these tables are stored, is there a file
    somewhere on the harddrive (assuming I’m running this locally)?

  2. How do ruby on rails apps work in general? There is way too much
    magic with all these generating scripts, it’s a little confusing. Let’s
    take a simple example: Can someone describe how you would go about
    creating a website with two input text fields and a button. The button
    would add up the numbers in the two text fields and display it and store
    it in a database table as an archive of answers that also get displayed
    in a table on the website. You don’t have to go into syntactic detail
    but simply describe the workflow and logical steps to take.

Sorry for the wordy post, and thanks for your time.

Go buy a book like RailSpace or some other Ruby on Rails book. These
will cover all this for ya, and it’ll be good to have around. I like
RailSpace just because it’s a tutorial for making a complete web app.

  1. you have to run a command called rake db:migrate to create your
    database tables. You must have a database installed yourself (like
    mysql). It’s located wherever you put it and you view it with whatever
    other program you install. You can use SQLite which is just a file on
    the harddisk, but i dunno how to view it.

Actually, just read this:

-Ryan

On Aug 3, 2:12 pm, Nebs P. [email protected]

kopf1988 wrote:

Go buy a book like RailSpace or some other Ruby on Rails book. These
will cover all this for ya, and it’ll be good to have around. I like
RailSpace just because it’s a tutorial for making a complete web app.

  1. you have to run a command called rake db:migrate to create your
    database tables. You must have a database installed yourself (like
    mysql). It’s located wherever you put it and you view it with whatever
    other program you install. You can use SQLite which is just a file on
    the harddisk, but i dunno how to view it.

Actually, just read this:
Radar – O’Reilly

-Ryan

On Aug 3, 2:12�pm, Nebs P. [email protected]

Hey,

Thanks for the reply. I considered getting a ruby on rails book but
then quickly started to realize that most of them are prior to 2.0 and
apparently some things just won’t work. I’d rather start learning with
something up to date.

I ran those command and I read that tutorial and many others like it.
My problem is not actually getting it to work (which I did), it’s
understanding how things fundamentally work so that I can make changes
when I want.

My question about the database wasn’t how to create one but where it is
physically situated on the harddisk and where ruby/rails holds relevant
information about said database. Running a command in the prompt is one
thing but I’d like to know how rails did this behind the scenes.

I’m new to this so it can get a little confusing when everything is done
automatically. I’m trying to read all the files rails generated for me
and put the pieces together (ie how do M, V and C communicate with
eachother). I understand the concept of MVC but I’m trying to
understand practically how they work. If I understand correctly the
“model” is the one dealing with the database, correct? I looked into
the “model” folder of a newly generate app using scaffold and the only
file in there is a empy method… Where is the communication with the
database happening if not in the model?

Sorry for my confusion, I’m hoping that with enough people replying I’ll
get a hold of things.

Will an old ruby on rails (pre 2.0) book give me understanding about RoR
but not be obsolete?

Thanks again.

On 3 Aug 2008, at 21:04, Nebs P. wrote:

My question about the database wasn’t how to create one but where it
is
physically situated on the harddisk and where ruby/rails holds
relevant
information about said database. Running a command in the prompt is
one
thing but I’d like to know how rails did this behind the scenes.

Well where the database is on disk depends on your database (in the
case of mysql on my machine it’s in /usr/local/mysql/data/).
Rails doesn’t store any info about it, apart from how to connect to it
(in config/database.yml)

database happening if not in the model?
It doesn’t need anything more. It knows the table name (because of the
convention for table/model names), and it knows how to connect to the
database.
The actual code for generating queries, executing them and dealing
with the results isn’t specific to your model at all (all that’s part
of activerecord itself)

Fred

Frederick C. wrote:

On 3 Aug 2008, at 21:04, Nebs P. wrote:
It doesn’t need anything more. It knows the table name (because of the
convention for table/model names), and it knows how to connect to the
database.
The actual code for generating queries, executing them and dealing
with the results isn’t specific to your model at all (all that’s part
of activerecord itself)

Fred

Perfect, thanks.

So the ruby file in the model folder defines the class of the type of
things stored in the database. eg. A table of Books will have a class
“Book” defined in the model’s ruby file?

The View holds html+ruby formatting files (ie the pages).

The Controller holds the logic used to process the data coming in from
the database (through model) and is then spit back out to the View which
displays it on screen.

Correct?

I created a project using the scaffold script to hold musicians and in
the view/musicians folder, the index.html.erb file has this line at the
end:

“<%= link_to ‘New musician’, new_musician_path %>”

Where is it defined that “new_musician_path” will link to new.html.erb?

This is the “new” method in the musicians_controller.rb:

def new
@musician = Musician.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @musician }
end

end

Since the “new.html.erb” is commented out, how does it know to go to
that page? Who is calling this “new” method? The model looks empty
other than what it inherits. As far as I know the View only receives
orders from the Controller so who’s accessing the controller methods?

I still can’t really see where the view and controller are
communicating.

If someone can give me a brief overview of how this MVC communication is
being done I’d be very greatful.

Thanks.

Phillip K. wrote:

Any decent book based on 1.2.x of Rails is not obsolete to a beginner.
Version 2.x has improved Rails, but it is not fundamentally different
than 1.2.x. A good book that walks you through building an app will
explain a lot of things to you and you will gain considerable
understanding.

Peace,
Phillip

Thanks for the info, appreciated. Do you have any recommendations of
good Ruby/Rails books? Something that’s geared towards people who have
experience with OOP but not specifically with ruby+rails. Are there any
2.0+ books out yet? I would hate to get used to some syntax/methods
only to find I have to relearn for 2.0

Nebs P. wrote:

Will an old ruby on rails (pre 2.0) book give me understanding about RoR
but not be obsolete?

Thanks again.

Hi Nebs

There has been a lot of discussion about the changes in Rails 2.x over
1.x and how the older books don’t work just right. There are things you
can do with 2.x installs to make the 1.x books work correctly, or you
can just install the older version of Rails (1.2.6) with

gem install rails -v 1.2.6

or something very similar to that. Then create a 1.2.6 application with

$> rails 1.2.6 my_cool_app

Any decent book based on 1.2.x of Rails is not obsolete to a beginner.
Version 2.x has improved Rails, but it is not fundamentally different
than 1.2.x. A good book that walks you through building an app will
explain a lot of things to you and you will gain considerable
understanding.

Peace,
Phillip

Hi Nebs,

Nebs P. wrote:

Will an old ruby on rails (pre 2.0) book give me
understanding about RoR but not be obsolete?

Yes. Rails 1.2.x made it both easy and fun to do something folks like
us
both need and want to do: develop web-based, database-driven
applications.
The major change from Rails 1.2.x to 2.x is making it ‘easy’ to develop
RESTful web-based, database-driven applications. I say ‘easy’ in quotes
because a) REST isn’t easy, and b) IMHO, Rails has become more
configuration-driven in 2.x which, by definition, takes some of the
‘easy’
out of it.

My recommendation is to learn Rails with 1.2.x and the books and
tutorials
out there that support it. If nothing else, the books will be cheaper
:wink:
Then, if you decide you need or want to learn REST, take on 2.x.

Just my $0.02

Bill

On 3 Aug 2008, at 21:43, Nebs P. wrote:

Fred

Perfect, thanks.

So the ruby file in the model folder defines the class of the type of
things stored in the database. eg. A table of Books will have a class
“Book” defined in the model’s ruby file?

correct

The View holds html+ruby formatting files (ie the pages).

The Controller holds the logic used to process the data coming in from
the database (through model) and is then spit back out to the View
which
displays it on screen.

Correct?

Yup. Although I’d push as much logic as possible into the model
(skinny controllers, fat models).
For example I would have a big chunky find call to retrieve some set
of musicians, that find call would be inside the model and the
controller would just do Musician.foo to get those musicians

I created a project using the scaffold script to hold musicians and in
the view/musicians folder, the index.html.erb file has this line at
the
end:

“<%= link_to ‘New musician’, new_musician_path %>”

Where is it defined that “new_musician_path” will link to
new.html.erb?

Well it doesn’t link to new.html.erb. It links to the action that
generates the form for a new musician. Methods like new_musician_path
are created when you declare routes in config/routes.rb

Since the “new.html.erb” is commented out, how does it know to go to
It’s not commented out in that it is just a comment. the default for
an action named foo is to render foo.html.erb and the comment is just
reminding you of this

that page? Who is calling this “new” method? The model looks empty
other than what it inherits. As far as I know the View only receives
orders from the Controller so who’s accessing the controller methods?

the piece of glue in between rails and whatever is hosting it (apache,
mongrel etc…) examines the url, pushes it through rail’s routing
stuff and decides which controller and action matches. An appropriate
controller is instantiated and setup and the appropriate method called.

Fred

Nebs P. wrote:

Thanks for the info, appreciated. Do you have any recommendations of
good Ruby/Rails books? Something that’s geared towards people who have
experience with OOP but not specifically with ruby+rails. Are there any
2.0+ books out yet? I would hate to get used to some syntax/methods
only to find I have to relearn for 2.0

There are 2.x books out. I haven’t read any of it yet, but a lot of
people say that The Rails Way is “the book” to get. When I got started
in Rails, I used Agile Web D. With Rails. It was “the book” to
get then. And since it was co-authored by the guy who originally created
Rails, I figured it would be accurate. There are other books available,
but I can’t comment on any of them.

The thing about Rails is even if you are an accomplished programmer, you
almost need to start at the very beginning because it’s such a different
way of thinking and approaching web development. Well, I guess I should
say that it was for me. In my limited experience with this forum, it
seems that those who don’t have programming knowledge in general
really struggle, while seasoned programmers can pick it up well
enough. But there is still a learning curve. Keep in mind you are
learning two things at once: Ruby, the language, and Rails, the
framework. If you already know Ruby, maybe Rails isn’t that tough.

Peace,
Phillip

[offlist]

I’m working on a Learning Rails book for O’Reilly that’s nearly done.
It’s not addressed at people coming from an OOP background
specifically, - but I’m hoping it might help with many of the rest of
your questions. I’ve shared a lot of your frustrations along the way,
and that frustration is what originally drove me to write this.

Let me know if you’d be interested in taking a look. We’re in tech
review now.

Thanks,
Simon St.Laurent
Editor, O’Reilly Media, Inc.

On Aug 3, 3:12 pm, Nebs P. [email protected]

Phillip K. wrote:

There are 2.x books out. I haven’t read any of it yet, but a lot of
people say that The Rails Way is “the book” to get.

Thanks, I’ll check that book out.

Frederick C. wrote:

Well it doesn’t link to new.html.erb. It links to the action that
generates the form for a new musician. Methods like new_musician_path
are created when you declare routes in config/routes.rb

It’s not commented out in that it is just a comment. the default for
an action named foo is to render foo.html.erb and the comment is just
reminding you of this

Thanks for the explanations. So it seems that rails is highly based on
naming conventions. I guess this is part of what makes it confusing to
understand since so much seems to be “implied” instead of being defined
explicitly.

This beast is clearly beyond simple analysis so I think I’m just going
to dive into a book and go to RoR pre-school.

Thanks to everyone for helping out.

Simon St.Laurent wrote:

Let me know if you’d be interested in taking a look. We’re in tech
review now.

Yeah that’d be interesting to read.