# Activerecord problem

**URL:** <https://www.ruby-forum.com/t/activerecord-problem/145242>\
**Category:** Rails\
**Created:** [August 7, 2008, 3:05am UTC](https://www.ruby-forum.com/t/activerecord-problem/145242 "2008-08-07T03:05:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@bishal](https://www.ruby-forum.com/u/bishal)\
**Post date:** [August 7, 2008, 3:05am UTC](https://www.ruby-forum.com/t/activerecord-problem/145242/1 "2008-08-07T03:05:03Z")

</div>

I have the following method…

def index  
@posts=Post.find(:all, :include =\> [:user, :comments])  
for post in @posts  
puts “Post: #{post.title}”  
puts “Written by: #{post.user.title}”  
puts “Last comment on: #{post.comments.first.created\_at}”  
end  
end

(the use model)  
class User \< ActiveRecord::Base  
has\_many:posts  
end

(the post model)

class Post \< ActiveRecord::Base  
belongs\_to:user  
has\_many :comments, :order =\> ‘created\_at DESC’  
end

when i run the application I get the following errors why ? Am i missing  
something ???

NoMethodError in TestController#index

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.title

RAILS\_ROOT: D:/InstantRails-2.0-win/rails\_apps/active  
Application Trace | Framework Trace | Full Trace

app/controllers/test\_controller.rb:6:in `index' app/controllers/test_controller.rb:4:in`each’  
app/controllers/test\_controller.rb:4:in `index’

---

<div class="post-metadata">

**Author:** ![bishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@bishal](https://www.ruby-forum.com/u/bishal)\
**Post date:** [August 7, 2008, 3:26am UTC](https://www.ruby-forum.com/t/activerecord-problem/145242/2 "2008-08-07T03:26:09Z")

</div>

On Aug 7, 2:05 am, Bishal A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> when i run the application I get the following errors why ? Am i missing  
> something ???

Looks like you’ve got a post without a user. Check what is actually in  
the database.

Fred

---

<div class="post-metadata">

**Author:** ![bishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@bishal](https://www.ruby-forum.com/u/bishal)\
**Post date:** [August 7, 2008, 3:36am UTC](https://www.ruby-forum.com/t/activerecord-problem/145242/3 "2008-08-07T03:36:51Z")

</div>

Frederick C. wrote:

> On Aug 7, 2:05ï¿½am, Bishal A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > end
> > 
> > when i run the application I get the following errors why ? Am i missing  
> > something ???
> 
> Looks like you’ve got a post without a user. Check what is actually in  
> the database.
> 
> Fred  
> Thanx for reply…but I have got both tables in tact with foreign key  
> reference as well…  
> users table has— id,login,email,title,content and other fields…  
> posts table has— id,title,content,user\_id, …and others… I guess  
> I have the foreign key reference ok…

---

<div class="post-metadata">

**Author:** ![bishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@bishal](https://www.ruby-forum.com/u/bishal)\
**Post date:** [August 7, 2008, 3:38am UTC](https://www.ruby-forum.com/t/activerecord-problem/145242/4 "2008-08-07T03:38:32Z")

</div>

Frederick C. wrote:

> On Aug 7, 2:05ï¿½am, Bishal A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > end
> > 
> > when i run the application I get the following errors why ? Am i missing  
> > something ???
> 
> Looks like you’ve got a post without a user. Check what is actually in  
> the database.
> 
> Fred  
> Thanx for reply…but I have got both tables in tact with foreign key  
> reference as well…  
> users table has— id,title,email,content and other fields…  
> posts table has— id,title,content,user\_id, …and others… I guess  
> I have the foreign key reference ok…

---

<div class="post-metadata">

**Author:** ![bishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@bishal](https://www.ruby-forum.com/u/bishal)\
**Post date:** [August 7, 2008, 3:43am UTC](https://www.ruby-forum.com/t/activerecord-problem/145242/5 "2008-08-07T03:43:46Z")

</div>

On Aug 7, 2:38 am, Bishal A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > the database.
> 
> > Fred
> 
> Thanx for reply…but I have got both tables in tact with foreign key  
> reference as well…  
> users table has— id,title,email,content and other fields…  
> posts table has— id,title,content,user\_id, …and others… I guess  
> I have the foreign key reference ok…

What you’re seeing suggests that your data isn’t quite what you think  
it is. Identify the post for which post.user is returning nil and  
workout why. unless you’ve got not null on user\_id, the foreign key  
constraint won’t prevent a null user\_id.

Fred
