views:

78

answers:

2

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 that in the console, if I do

User.first.sent_posts.empty?

it returns True.

But if I do this in my view

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

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

has_many :posts

on its own.

Thanks

+1  A: 

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

cpjolicoeur
set the `@user = User.first` in the relevant action in the controller if not..
Gishu
Yep I have only that one record in the DB right now...
Cameron
A: 

Figured it out. It was to do with having an @user.sent_posts.build in the controller! Doh.

Cameron