Adding facebook-ish style comments to microposts from the Rails on Ruby Tutorial - ruby-on-rails

First and foremost, I am brand new to Ruby on Rails and very rusty when it comes to programming. I have done some research but I am still trying to learn the basic inner workings of RoR. I have completed the Ruby on Rails Tutorial by MHartl.
What I am attempting to do to expand my knowledge of Ruby on Rails is to add a comment function to the Microposts. I have tried to simply add a comment column into the db and then work it into the already functioning microposts. I have also tried to generate a new model called commments using the following line:
rails generate model Comment content:string user_id:integer micropost_id:integer
I think I may have been on to something with that last one but I am hitting a road block when it comes to implementing it. Can someone please point me in the right direction?

You may want to look into Rails associations between models which allow you to associate one record to others. I would recommend looking over the following resources:
A Guide to Active Record Associations
Cardinality and associations
ActiveRecord::Associations::ClassMethods
Your close and I think that looking through the Rails Guide will get you up to speed on what types of relations you want to define. If you need more hints just ask.

Related

Scaffold creates too many files for me. What should I do?

I'm new in the world of Ruby on Rails. I have some problems to solve before start to build a store-like web application.
I'm following instructions written in the book "Agile Web Development with Rails" so I decided to use sqlite too...
...but I have already represented the scenario via ER Diagrams and now I don't know how to bring it on rails.
In the early chapters of the book, it uses the scaffold command to create the table "Product". But this command creates model, view, controller and test for every table I want to represent.
Is it the right way to proceed? Or is there a way to build all my tables before I start to create mvc I need?
Try the Guides to Ruby On Rails:
http://guides.rubyonrails.org/getting_started.html
It's generally easier to understand (in my opinion) than the Agile book.
The easiest way to create your tables is by using Rails migrations.
The easiest way to create migrations with the related models, views, and controllers is:
rails generate scaffold Product
That command will print what it's doing. Take a look in those files.
If you have many tables, yes the typical way is to generate many scaffolds, for example:
rails generate scaffold User
rails generate scaffold Product
rails generate scaffold Company
rails generate scaffold Invoice
...
In your comment you ask about a type_of_product table. For tables like these, yes it's fine to skip the scaffold (for example because you don't need a controller) and instead just generate a migration:
rails generate migration TypeOfProduct
Heads up that Rails does odd things with the word "type". When I did a table like that, I found it easier to start with the main word then use the word "kind" like this:
rails generate migration ProductKind
Short answer: YES, this is the right way.
Longer answer: Usually you want to design a model, then design the next one, so usually this is the right way. If you create a table without a model, there is no value in pure existence of a table.
If you want to create only tables, without a model, you may create migration. Just use ./script/rails generate migration CreateSomeTable.
In any case you may just ignore the created files which you do not need right now. Just let them stay where they are, and focus on all the tables (migrations) if you wish so.
Scaffolding gives you a great starting point for working with a resource, such as a user, post, article, etc. It generates models, views and controllers and then you can focus on modifying it to fit your needs.
Your entity relationship diagram gives you a great roadmap for building your system. You shouldn't be worrying about big banging 20 tables into the app at once. You should, instead, build them out one and a time (through scaffolding or the other rails generators for models and migrations) working on them until they are right, then building out the other tables and relationships one at a time.
Work small and you'll be incredibly productive. Work large and you'll spend a lot of wasted time trying to figure out where you went wrong in a single giant operation.

Advanced Ruby on Rails database?

Im taking on an upcoming project that involved creating and managine a large complex mysql database using RoR. It involved, many table, deep foreign keys, many to many, etc.
Anyone know a good resource (book, website, etc.) which can help me learn how to do it? I need a clearer understanding of migrations and how rails handles relational databases.
Any suggestions?
railscasts.com is great. He talks a lot about setting up your typical HABTM relations polymorphic associations, inner nestings you name it. and Bates makes it extra easy to understand.
Past that I would definately try to start off on the right food using the Metrics gem to monitor database and CPU usage. Always ensuring you're making the lightest touch on the db.
Besides RailsCast, I would recommend RailsLab: Scaling Rails which focus on large Rails application. It seems doesn't update recently, but the post still useful, not only database, but also memory, performance etc.
+1 for Railscasts.com suggestion from Trip. I've gotten a ton out of those since I moved to Ruby and Rails from ColdFusion. I also like the book "The Rails 3 Way" from Obie Fernandez. It has an extensive ActiveRecord section that is very informative both for new Rails users and is a great reference. Another solid one is "Rails 3 In Action" from Ryan BIgg and Yehuda Katz.
You can check out books at the Pragmatic Programmers website. Probably one of the best options is: Agile Web Development with Rails
I also found Ruby on Rails Tutorial to be a good resource.
Also be sure to look at the Rails Guides They are well written and cover many topics in depth. For example, the migrations you mentioned and many to many associations You can even download them for offline use. Here is an answer about how to do that here
Code School have some great interactive courses such as Rails for Zombies.

Tutorial teaching Ruby syntax/knowledge required to easily learn rails

I am trying to learn ROR these days and have basic knowledge of ruby, but often working with rails, I get to the point where it seems as if I don't know a bit about ruby.
Just to explain the point, in rails we use has_many keyword. I did not learn any such thing when I was going through ruby tutorials but just came to know that it has something to do with meta-programming in ruby (I have no idea what is meta programming).
So I would like to know if there is any book/tutorial which explain all the points/syntax/concepts of ruby, which a newbie would see while programming in rails.
Thanks to "Jonas Elfström", in simple words, what I am looking for is to know "how Rails uses Ruby"
Thanks.
has_many isn't a keyword, it's simply a class method in the ActiveRecord::Associations module.
It's documented here and you can even view the source if you scroll down a bit.
Associations are a set of macro-like class methods for tying objects
together through foreign keys. They express relationships like
“Project has one Project Manager” or “Project belongs to a Portfolio”.
Each macro adds a number of methods to the class which are specialized
according to the collection or association symbol and the options
hash. It works much the same way as Ruby’s own attr* methods.
If you already know Ruby the Rails Guides could get you going but it might be easier to learn from one of the books listed at the documentation page or by watching a couple of screencasts.
For books about Ruby I've never seen such praise as what Eloquent Ruby gets.
There's a tutorial called Learn Ruby the Hard Way
(written by Rob Sobers of Fog Creek Software)
I tried lately this: http://pine.fm/LearnToProgram/ which helped me a lot because there are one or two concepts that had slipped. I started long ago with an arcane version of this.

rollback generated controller/model in RoR

I created, using the scaffolding, a model and controller files.
Later I discovered it would be a good idea to create the tables in the DB first...
My question, How can I role back the generated files and regenerate them now, that I have the tables in the DB?
I just started learning RoR, so right now I am not interested in best practices, just learning the tool box this FW (RoR) comes with.
And, do you have a recommendation for a good tutorial? I do know to use google, it is just that search engines don't know, yet (working on that), how to grade tutorials.
Edit: For my last question I found Learning Ruby on Rails
try
rails destroy scaffold XXXXX
one thing that I find puzzling though is that you said "Later I discovered it would be a good idea to create the tables in the DB first..."
Well, rails creates a migration file for you when you run the generator in the first place, and this file will create your DB tables and fields when you run it using rake db:migrate
PS - here's a few good tutorials for you:
rails tutorial ebook
rails demo site
rails 4 zombies
You can rollback controller.
rails destroy controller [controller]
You can delete all the files Rails created -- just look at the printout on your command line, see what files rails created, and delete them.
I don't know why you would want to create all the tables in the db, but that's fine, I guess. I prefer to let rails do it. Either way, Rails won't mind. You can always add / change fields using Rails, even if you created the tables outside Rails.
Ryan Bates' Railscasts are excellent tutorials.

Subscribe to newsletter form rails 3

I'm new to rails and I would like to know how to make a form that submits to a database. I have tried devise but it seems that it deals with logins/users and it's not working for my purpose. I don't want anyone to do it for me, I have to learn :).
Creating a form that saves to a database is probably on of the most basic tasks to do with rails and is even handled in the "Getting Started"-Chapter in the Rails-Guides:
http://guides.rubyonrails.org/getting_started.html
Just work on that and you'll get the idea. It looks like it's a lot to do on that page, but it explains also the principles and all that. It becomes really interesting for you in chapter 5 and 6.
Creating a form With Rails 3 is exactly like in Rails 2.

Resources