PDA

View Full Version : Blog database design


Frequency
July 2nd, 2005, 02:03 AM
I'm trying to create a blog on my site and I've set up the database with tables for entries and comments. I'm very new to Rails (this is my first Rails app) and I've no idea how to get the information from the tables into the application.

I've set up a controller for the blog. When I go to /blog on my site, I want it to show the most recent X entries (probably 10), and I want to be able to view specific entries by going to /blog/entryid or /blog/entry/entryid or whatever. Maybe comments at /blog/comment/add. Maybe the way the Rails framework works just hasn't fully sunk in yet, but all tutorials I've found are way too simple and I can't seem to figure out how to adapt the information in them to what I need.

If anybody could help, I'd appreciate it greatly. Thank you so much.

Jamie..

bluetechnx
July 2nd, 2005, 03:23 AM
I'm fairly new to Rails myself. But, let's assume you have a database table with the following attributes:


entries
id, content, comment_id, time_of

comments
id, content, time_of

This way blog entries can contain text content and any number of comments from users. For useful data we can also store a date for every entry and individual comments.

I believe listing the most recent 10 entries could be done simply by:

# in blog_controller.rb

# default index action for url.
def index
list
render_action 'list'
end

# list 10 most recent entries
def list
@blog_pages, @entries = paginate :entry, :order_by "id ORD_DESC" :per_page => 10
end


To find a blog entry by entryid (in Rails the entry id is usually just "id") you would want to show the contents of Entry.find(@params[:id]. You could do this by getting it's data and showing it to the screen using rhtml, for example.


# load the desired entry, to be shown in a view asap
def show
@entry = Entry.find(@params[:id])
# below or, entry.comments ...I believe
@comments = Comment.find[@entry.comment_id]
end

# in views/entries/show.rhtml
...pretty html code to make your page look nice...
<% @entry.message %>
<%= for comment in @comments %>
<% comment.text %>
<%end%>


Should display each entry and any related comments...

So hopefully this helps. Try to remember the MVC pattern, and try not to get too fancy/complex. Please excuse the above for any typos :). The model represents the data and relationships between them (entry and comments) and the controller handles the in-between job of fetching and preparing the data for the view...