Mark Chandler
2006-Aug-17 18:34 UTC
[Rails] super-newb needs help with has_many and other things...
I am brand new to oop and am having a difficult time getting my head
around some ruby stuff. In specific, I am trying to create a library
style app that lists topics, articles and comments. I have a table
called topics, a table called articles and a table called comments. My
models, controller and view are all listed below. What I want to happen
is to have the list view list each topic along with the names of each
article below it. This app will not be used for anything, I am just
trying to get a feel for Ruby. I don''t have a clue where to start doing
this and any help would be much appreciated. I really feel like I am
spinning my wheels, but a good solid push might get me on track. Thanks!
***** Models: *****
class Topic < ActiveRecord::Base
has_many :articles
validates_presence_of :name, :creator, :creationdate
end
class Article < ActiveRecord::Base
has_many :comments
belongs_to :topic
end
class Comment < ActiveRecord::Base
belongs_to :topic
end
***** Controller: *****
class AdminController < ApplicationController
def index
list
render :action => ''list''
end
# GETs should be safe (see
http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@topic_pages, @topics = paginate :topics, :per_page => 10
end
def show
@topic = Topic.find(params[:id])
end
def new
@topic = Topic.new
end
def create
@topic = Topic.new(params[:topic])
if @topic.save
flash[:notice] = ''Topic was successfully created.''
redirect_to :action => ''list''
else
render :action => ''new''
end
end
def edit
@topic = Topic.find(params[:id])
end
def update
@topic = Topic.find(params[:id])
if @topic.update_attributes(params[:topic])
flash[:notice] = ''Topic was successfully updated.''
redirect_to :action => ''show'', :id => @topic
else
render :action => ''edit''
end
end
def destroy
Topic.find(params[:id]).destroy
redirect_to :action => ''list''
end
end
***** View: *****
<h1>All Topics:</h1>
<table>
<% for topic in @topics %>
<tr><td><b><%=h(topic.name)
%></b><br><%=h(topic.description)
%></td></tr>
<tr>
<td><%= link_to ''Show'', :action =>
''show'', :id => topic %>
<%= link_to ''Edit'', :action =>
''edit'', :id => topic %>
<%= link_to ''Destroy'', { :action =>
''destroy'', :id => topic },
:confirm => ''Are you sure? This will Destroy the topic and all
related
articles. This may be a poor idea...'', :post => true
%></td></tr>
<% end %>
</table>
<%= link_to ''Previous page'', { :page =>
@topic_pages.current.previous }
if @topic_pages.current.previous %>
<%= link_to ''Next page'', { :page =>
@topic_pages.current.next } if
@topic_pages.current.next %>
<br>
<%= link_to ''New topic'', :action =>
''new'' %>
***** Migrations: *****
001:
class CreateTopics < ActiveRecord::Migration
def self.up
create_table :topics do |t|
t.column :name, :string, :null => false
t.column :description, :string
t.column :creator, :string, :null => false
t.column :creationdate, :timestamp, :null => false
end
end
def self.down
drop_table :topics
end
end
002:
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.column :name, :string, :null => false
t.column :description, :string
t.column :body, :text, :null => false
t.column :creator, :string, :null => false
t.column :creationdate, :timestamp, :null => false
t.column :topicid, :integer, :null => false
end
execute "alter table articles
add constraint fk_article_topics
foreign key(topicid) references topics(id)"
end
def self.down
drop_table :articles
end
end
003:
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.column :comment, :text, :null => false
t.column :creator, :string, :null => false
t.column :creationdate, :timestamp, :null => false
t.column :articleid, :integer, :null => false
end
execute "alter table comments
add constraint fk_comment_articles
foreign key(articleid) references articles(id)"
end
def self.down
drop_table :comments
end
end
--
Posted via http://www.ruby-forum.com/.
