Topic: Beginner - needs help with simple database

Hello guys,
I am a complete beginner in Rails and I am suppose to create a simple database that "adds, edits and deletes categories and also adds, edits and deletes their subcategories". Also, the subcategories should be able to add comments and that comments should be attached to another clear page.  I have already spent two days and countless hours on this following many tutorials but I always get only errors, nothing else. I am desperate because I need to finish it by the end of next week and it just seems impossible right now.

Please, if you write... try to explain it clearly step by step as I am really learning from the scratch.

Many thanks

Faith

Last edited by faith (2011-11-30 12:15:01)

Re: Beginner - needs help with simple database

1. You following suggestion is absolutely incorrect:

I am suppose to create a simple database that "adds, edits and deletes categories and also adds, edits and deletes their subcategories"

a database does not create, edit nor update anything, - it's your code in your application which should do that.

2. I have no idea which tutorial you followed and which errors you had (no stack trace, no description, etc.), but if you follow (not by diagonal, but line by line and step by step)  the one on the Rails API page, you should be done:

http://api.rubyonrails.org/

Then look Rails guides for more details: http://guides.rubyonrails.org/

Later see Rails screen casts: http://railscasts.com/

You should always have in front of you:

Ruby API: http://ruby-doc.org/core-1.9.3/
Rails API: http://api.rubyonrails.org/

And if you have some questions or problems, come back later and post the detailed description.

Last edited by Javix (2011-12-01 04:56:29)

Re: Beginner - needs help with simple database

Actually after 2.5 days of seeing every single tutorial on the web I have nothing but only two tables called categories and items that I have no idea how to link. I used has many and belongs to many scripts in" models" under "app" directories. It didnt make a difference, it's not linking. I dont know why. Does it have anything with parent id? I tried it as well, it does not respond, only shows the number of the category on the web.

Re: Beginner - needs help with simple database

Relationships are dead simple

parent model has many children and child model belongs to parent
the child needs a column in the database named after the parent with _id suffix of type integer

e.g.

class Category ...
  has_many :items
  ...
end

class Item ...
#This MUST have a category_id of type integer in the database
  belongs_to :category
  ...
end

To link 2 records in a relationship together

  @category = Category.find(params[:category_id]
  @item = @category.build
  @item.some_value = 'Something'
  @item.save

To use the relationship
@parent.children.each do |child|
  #do something with the child here
end

Pay more attention to the guides that Javix pointed out to you and watch all the railscasts - you will pick up loads of tips and tricks

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)