Topic: Basic Database / relationship-question.

Hi. Im new here, well; im actually new to the whole rails-community!
I've looked at a couple of books so I should know pretty much, but that's not the case smile

Im having problem with the whole ActiveRecord class, adding data, recieve data, and relationships.

Im gonna do some pseudocode since im not at my developer-machine at the moment, I hope that's alright.

What i want to do is simple:

I want to add text and comments to a database and in "list.rhtml" list _all_ posts and at "show.rhtml" list all comments for the right post.
I started to by creating two tables and two models.
Posts (id, message) - post.rb:
has_many :comments
---------------------
Comments (id, comment, post_id) - comment.rb
belongs_to :post

Then i added my first Post:

def add
@posts = Post.new(params[:post])
end

Here is when satan came and destroyed my vision. I just wanted to make a pretty list
of my Posts, I added following code:
def list
    @posts = Post.find(:all)
end

(For some reason my comments also will end up in this list?)

Now it's time to add a comment for that post, right.

def comment
    @post = Post.find(params[:id])
end

That's for telling the comment to interact with the right Post-id, Im not sure if that's correct, anyway.
I now got another problem when I tried to add the comment data.
def SaveComment
    @comment = Comment.new(:post_if => post, :message => params[:message] (*See below)
end

*I think that params[:message] (it's supposed to pickup my text i entered in the "message"-textbox) is wrong, because if i do:
:message => "add text manual"

it works a little better but even now i sometimes get a string with this:
"!MAP-error, etc, etc, etc *add text manual*". So it's not very pretty.

Im not really sure what im asking for here, it's just that i thought i've read enough to complete a simple thing like this but i guess i was wrong.

Maybe someone can give me a hint or maybe im just not meant to work with rails!

PS - Sorry for the long post.

Have a great day!

Re: Basic Database / relationship-question.

Emil wrote:

I started to by creating two tables and two models.
Posts (id, message) - post.rb:
has_many :comments
---------------------
Comments (id, comment, post_id) - comment.rb
belongs_to :post

Looks good.




Emil wrote:

Then i added my first Post:

def add
@posts = Post.new(params[:post])
end

Here is when satan came and destroyed my vision. I just wanted to make a pretty list
of my Posts, I added following code:
def list
    @posts = Post.find(:all)
end

(For some reason my comments also will end up in this list?)

Seems correct so far. Usually the "add" action is called "new" though.

Emil wrote:

Now it's time to add a comment for that post, right.

def comment
    @post = Post.find(params[:id])
end

That's for telling the comment to interact with the right Post-id, Im not sure if that's correct, anyway.

Not sure what you are trying to do here. The method doesn't have any mention of "comment" except for the method name. Is this action supposed to display a comment?


Emil wrote:

I now got another problem when I tried to add the comment data.

def SaveComment
    @comment = Comment.new(:post_if => post, :message => params[:message] (*See below)
end

*I think that params[:message] (it's supposed to pickup my text i entered in the "message"-textbox) is wrong, because if i do:
:message => "add text manual"

I'm guessing you are using the "text_area" method in your view for displaying the text block. Try using text_area_tag instead. It should submit the message in params[:message] as you are trying to here. There are several other problems with the code, but try that and see if you can get it working with text_area_tag.

Railscasts - Free Ruby on Rails Screencasts

Re: Basic Database / relationship-question.

Hi Ryan and thanks for your post.

ryanb wrote:
Emil wrote:

Now it's time to add a comment for that post, right.

def comment
    @post = Post.find(params[:id])
end

Not sure what you are trying to do here. The method doesn't have any mention of "comment" except for the method name. Is this action supposed to display a comment?

I thought that was the best way to define the id. Since i go from:
"/post/id" to "/comment/id".

Thanks. /Emilio

Edit:
It works now, It just feels so damn ugly. I did screw up the text_fields and the post_id. (Which I still really don't understand). I guess I could ask here;
 

def show
    @post = Post.find(params[:id])
end

That's my show-view of the post + it's here you post comments.
 <% form_for :comment, :url => { :action => :comment, :id => @post }

That's the form for posting a comment. (/blog/comment/*id of blog-entry*)
  def comment
    @comment = Comment.create(:post_id => params[:id], :comment => params[:comment])
  end

This is the end, here is where my comment is stored to the database.
Question:
Why can't I get the post id like this: :post_id => post, instead of the params[:id], I did set the variable?

Last edited by Emil (2006-08-28 08:16:18)

Re: Basic Database / relationship-question.

Looks like you're almost there. The @post variable isn't available in the comment action because you need to recreate it for each action. You can do something like this for adding a comment to a post:

def comment
  @post = Post.find(params[:id])
  @comment = @post.comments.create(params[:comment])
end

I think that's right anyway. Hope that works.

Railscasts - Free Ruby on Rails Screencasts

Re: Basic Database / relationship-question.

ryanb wrote:

Looks like you're almost there. The @post variable isn't available in the comment action because you need to recreate it for each action. You can do something like this for adding a comment to a post:

def comment
  @post = Post.find(params[:id])
  @comment = @post.comments.create(params[:comment])
end

I think that's right anyway. Hope that works.

Thanks alot, that worked. Im starting to understand this now, thanks again for all of your help!