We can of course give random examples, but if you give us more information of the types of websites you want to create we can give specific examples.
Random example:
Code:
class Category < ActiveRecord::Base
acts_as_tree
end
programming = Category.create(:name => 'Programming')
ruby = programming.children.create(:name => 'Ruby')
php = programming.children.create(:name => 'PHP')
rails = ruby.children.create(:name => 'Rails')
This creates a category tree (it's stored in a database table called `categories`).
You can use it like this: (#=> means "returns")
Code:
category = Category.find_by_name('Ruby')
category.children #=> [#<Category:0xb70a83ec @attributes={"name"=>"Rails","id"=>4,"parent_id"=>2}>]
Note that you get a rich and easy to use API with just 1 line that says `acts_as_tree`.
Code:
class Category < ActiveRecord::Base
acts_as_tree
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :category
end
agile = rails.books.create(:title => 'Agile Web Development with Rails', :author => 'Dave Thomas')
sitepoint = rails.books.create(:title => 'Build Your Own Ruby On Rails Web Applications', :author => 'Patrick Lenz')
pickaxe = ruby.books.create(:title => 'Programming Ruby', :author => 'Dave Thomas')
agile.category #=> #<Category..."Rails"...>
rails.books.find_by_author('Dave Thomas') #=> #<Book... "Agile Web Development With Rails">
ruby.books.find_by_author('Dave Thomas') #=> #<Book..."Programming Ruby">
Bookmarks