vijayalakshmi
Joined: 27 Nov 2009 Posts: 38
|
Posted: Wed Jun 30, 2010 10:01 am Post subject: Using belongs_to and has_many relations in rails app |
|
|
Hi friends,
Lets us have a brief introduction about belongs_to and has_many, before using them in rails application.
belongs_to:
~~~~~~~~~~
belongs_to is an association that sets up a one-to-one connection with
another model. So, each instance of the declaring model “belongs to”
one instance of the other model.
has_many:
~~~~~~~~~~
has_many association sets up a one-to-many connection with another
model. This indicates that each instance of the model has zero or more
instances of another model. In-short we can say has_many association
on the “other side” of a belongs_to association.
Now, implement these relations in a sample rails application.
Let us consider two models Book and Comment where a book may
contain any no.of comments and each comment belongs to one book.
Create a rails application, and generate two models named 'book' and 'comment' and follow the following files.
app/model/book.rb
~~~~~~~~~~~~~~~~~
class Book < ActiveRecord::Base
has_many :comments
end
app/models/comment.rb
~~~~~~~~~~~~~~~~~~~~~
class Comment < ActiveRecord::Base
belongs_to :book
end
And our migration files should be:
20....._create_books.rb
~~~~~~~~~~~~~~~~~~
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :title
t.text :description
t.timestamps
end
end
def self.down
drop_table :books
end
end
20..._create_comments.rb
~~~~~~~~~~~~~~~~~~~~~~~
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.text :text
t.string :author
t.belongs_to :book
t.timestamps
end
end
def self.down
drop_table :comments
end
end
Now create controllers for books and comments,
app/controllers/comments_controller.rb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CommentsController < ApplicationController
def create
book = Book.find params[:book_id]
comment = book.comments.new params[:comment]
comment.save
flash[:notice] = 'Comment saved'
redirect_to book_path(book)
end
end
app/controllers/books_controllers.rb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class BooksController < ApplicationController
def index
@books = Book.paginate :page => params[:page], :per_page => 3
end
def show @book = Book.find(params[:id])
@comments = @book.comments
end
def new
@book = Book.new
end
def edit
@book = Book.find(params[:id])
end
def create
@book = Book.new params[:book]
if @book.save
flash[:notice] = "#{@book.title} saved."
redirect_to @book
else
render :new
end
end
def update
@book = Book.find params[:id]
if @book.update_attributes(params[:book])
flash[:notice] = "#{@book.title} saved."
redirect_to @book
else
render :edit
end
end
def destroy
book = Book.find params[:id]
book.destroy
flash[:notice] = "#{book.title} deleted."
redirect_to books_path
end
end
And here comes to our views folder,
app/views/books/new.html.erb
~~~~~~~~~~~~~~~~~~~~~
<h1>New book</h1>
<% form_for(@book) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description, "rows"=>10,"cols"=>50 %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', books_path %>
app/views/books/show.html.erb:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<h1><%=@book.title%></h1>
<%= simple_format @book.description %>
<% if @comments %>
<h3>Comments:</h3>
<% for comment in @comments do %>
<p><strong><%=h(comment.author) %></strong>:</p>
<%= simple_format comment.text %>
<% end %>
<% end %>
<h3>Post Your Comment</h3>
<% form_for([@book, Comment.new]) do |form| %>
<p>Your name:</p>
<p><%= form.text_field :author %></p>
<p><%= form.label :text, 'Comment' %></p>
<p><%= form.text_area :text ,"rows"=>8,"cols"=>45%></p>
<%= form.submit 'Save' %>
<% end %>
<p>
<%= link_to 'Back', books_path %>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Delete', book_path(@book), :method => :delete, :confirm =>
"Are you sure?" %>
</p>
And make sure that config/routes.rb containing the following code:
config/ routes.rb
~~~~~~~~~~~~
map.resources :books do |book|
book.resources :comments, :only => :create
end
map.root :controller => 'books', :action => 'index'
You Can start the server and view our application,
http://localhost:3001
Thanks,
Vijayalakshmi. |
|