views:

120

answers:

2

Hey all,

Following this tutorial "http://allaboutruby.wordpress.com/2009/08/08/5-minute-project-in-rails/" and i cannot get past the error in the subject.

It happens when i modify "app/views/posts/show.html.erb" according to the tuorial.

Now i have got it working another way previously (another tutorial) but had to put something like @users = user.find_all in the posts controller.

My question is - without modifying the controllers, only adding the relationships to the models - can you still use something like "post.user.name" like the tutorial in quesiton.

I am a noob - but would it have something to do with the User table not having a user_id ? so how the hell can the post's table get it link to the user table ?

Can anyone do this tutorial and actually get it to work ? Does it have something to do with my using RUBY-1.8.6-27 and not the latest 2.x.x?

I don't know. I need to sort this out as it will be used HEAVILY in the app i want to make.

+1  A: 

When you created your migration by using this command:

ruby script/generate migration add_user_id_to_post user_id:integer

The migration script knows from your migration name to add a user_id to the Post model. The user_id:integer is the standard ActiveRecord::Migration code for defining a user_id that is an integer.

You will see a migration that is generated with the following code:

class AddUserIdToPost < ActiveRecord::Migration
  def self.up
    add_column :posts, :user_id, :integer
  end

  def self.down
    remove_column :posts, :user_id
  end
end

After you need to run:

rake db:migrate

Check your database to see the user_id is present in the Post table. Otherwise post.user.name will not work

Spasm
A: 

Yep - did all that.

I got a different example working all OK - think it might have something to do with the user table not having an actual column called "id" ... as the one where i have working does.

Following that example (tutorial) - it wont work. You're saying it should ? anything to do with the version of ruby or rails ? I'm using Windows - Ruby v 1.8.6 and rails 2.3.4 - with mysql plugin for the DB obviously.

Shannon
i am using the same setup as you and it worked fine for me. If you have user_id in your migration the it will work. Rails by default adds an id column as a primary key by default for you. The only way to turn it off is to have a create_table method with :id => false in it?
Spasm