Reputation:
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.
Upvotes: 0
Views: 1153
Reputation:
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.
Upvotes: 0
Reputation: 805
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
Upvotes: 1