In rails console doing User.first and Post.first works fine. Post.first.user works fine but User.first.posts gives me an Undefined method error
The models:
class User < ActiveRecord::Base
attr_accessible :role :user_email, :user_name
has_many :posts
end
and
class Post < ActiveRecord::Base
attr_accessible :content, :status, :title, :user_id
belongs_to :user
has_many :comments
end
When I create a new project everything works fine but in this case I did something different. I adapted an existing database (sqlite) from another web application (non-rails) to the rails. What I did was rename columns to fit rails conventions and where previously I was using user name as unique key and used that to link to post owners I changed the column name to user_id and updated all the values to the relevant user id. I changed the column type and values to int values too.
Same goes for Comment: Comment.first.post works but Post.commments gives the undefined method error.
When doing User.joins(:post(s)) I get the error "Association named 'posts' was not found" but Post.joins(:user) works fine.
Edit: Shame on me - I left out relevant information I didn't think was relevant.
Anyway - I had a column called role so the User model had attr_accessible :role --- once > I removed that everything worked like intended.
I however don't understand why this should make a difference. This: > https://github.com/ryanb/cancan/wiki/Role-Based-Authorization user model has :role and I > would assume any user based app would have has_many dependency and thus fail like mine did.
... or is there something else I'm missing?