0
votes

I'm new to rails and am learning how to use it.

I came across this problem which i can't figure out what the problem is.

i have 2 classes:

class User < ActiveRecord::Base has_one :blog end

class Blog < ActiveRecord::Base belongs_to :user end

When i try to do User.first.blogs or User.first.blog (not sure which is correct), it tells me that

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: blogs.user_id: SELECT "blogs".* FROM "blogs" WHERE ("blogs".user_id = 1)

I really don't know why there's no such column like that. To my knowledge, i think active record is supposed to handle that for me, right?

1

1 Answers

1
votes

you have to add the foreign key column(user_id) in blogs table. when you specified the association in rails, you need to add foreign_key column into associated table. look here for complete document on rails associations