Has_many class name not working as I thought?

I have a user and post model:

class User < ActiveRecord::Base
  has_many :sent_posts, :class_name => 'Post'
end

class Post < ActiveRecord::Base
  belongs_to :user
end

      

The problem is, in the console, if I do

User.first.sent_posts.empty?

      

it returns True.

But if I do it from my point of view

<%= @user.sent_posts.empty? %>

      

it returns False. Any idea why this is happening? It works great if I just use

has_many :posts

      

yourself.

thanks

0


source to share


2 answers


Found it out. This is because there is @ user.sent_posts.build in the controller! Doh.



0


source


Are you sure User.first and @user are the same user model entry?



+1


source







All Articles