# Find Last Errors

**URL:** <https://discuss.rubyonrails.org/t/find-last-errors/50077>\
**Category:** rubyonrails-talk\
**Created:** [March 11, 2010, 6:56pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077 "2010-03-11T18:56:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 11, 2010, 6:56pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/1 "2010-03-11T18:56:54Z")

</div>

Hi guys: This is driving me crazy. Can someone please point me in the right direction:

I'm trying to get the last record from my table. Every time I do this, it tells me there is no method, other than id.

theconditions = "criteria = '"+t+"'" lastrec = Capture.last(:all, :conditions =\> theconditions)

This table has a number of fields in it id, created\_at, tid, text

When I call lastrec.id, it works When I call lastrec.created\_at it gives me (undefined method `created\_at' for nil:NilClass)

Any ideas? Thanks for your help!

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 11, 2010, 7:06pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/2 "2010-03-11T19:06:41Z")

</div>

Chris Kalaboukis wrote:

> Hi guys: This is driving me crazy. Can someone please point me in the right direction:
> 
> When I call lastrec.id, it works When I call lastrec.created\_at it gives me (undefined method `created\_at' for nil:NilClass)
> 
> Any ideas? Thanks for your help!

If you look in your database, what does this id match to?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 11, 2010, 7:18pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/3 "2010-03-11T19:18:55Z")

</div>

Aldric Giacomoni wrote:

> Chris Kalaboukis wrote:
> 
> > Hi guys: This is driving me crazy. Can someone please point me in the right direction:
> > 
> > When I call lastrec.id, it works When I call lastrec.created\_at it gives me (undefined method `created\_at' for nil:NilClass)
> > 
> > Any ideas? Thanks for your help!
> 
> If you look in your database, what does this id match to?

There is a record there, with that ID. I just can't seem to call it up with a last command...

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 11, 2010, 7:21pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/4 "2010-03-11T19:21:33Z")

</div>

> Hi guys: This is driving me crazy. Can someone please point me in the right direction:
> 
> I'm trying to get the last record from my table. Every time I do this, it tells me there is no method, other than id.
> 
> theconditions = "criteria = '"+t+"'" lastrec = Capture.last(:all, :conditions =\> theconditions)
> 
> This table has a number of fields in it id, created\_at, tid, text
> 
> When I call lastrec.id, it works When I call lastrec.created\_at it gives me (undefined method `created\_at' for nil:NilClass)

I feel sure you must be mistaken somehow. Are you saying that if you immediately follow the Capture.last line with id = lastrec.id created\_at = lastrec.created\_at then the first line passes but you get the nil error on the second? I will be surprised if that is the case (I am often surprised mind you).

One thing to try is to run that code in the ruby console. Another is to use ruby-debug to break in after the Capture.last line and examine the data at that point. See the rails guide on debugging if you do not know about ruby-debug ([http://guides.rubyonrails.org/](http://guides.rubyonrails.org/))

Colin

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [March 11, 2010, 8:42pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/5 "2010-03-11T20:42:54Z")

</div>

> > Hi guys: This is driving me crazy. Can someone please point me in the right direction:
> > 
> > I'm trying to get the last record from my table. Every time I do this, it tells me there is no method, other than id.
> > 
> > theconditions = "criteria = '"+t+"'" lastrec = Capture.last(:all, :conditions =\> theconditions)
> > 
> > This table has a number of fields in it id, created\_at, tid, text
> > 
> > When I call lastrec.id, it works When I call lastrec.created\_at it gives me (undefined method `created\_at' for nil:NilClass)
> 
> I feel sure you must be mistaken somehow. Are you saying that if you immediately follow the Capture.last line with id = lastrec.id created\_at = lastrec.created\_at then the first line passes but you get the nil error on the second? I will be surprised if that is the case (I am often surprised mind you).
> 
> One thing to try is to run that code in the ruby console. Another is to use ruby-debug to break in after the Capture.last line and examine the data at that point. See the rails guide on debugging if you do not know about ruby-debug ([http://guides.rubyonrails.org/](http://guides.rubyonrails.org/))
> 
> Colin

Hmm, does the id happen to be 4? 😉

try:

> puts nil.id

Last relative to what? If you mean: most recently created, try:

lastrec = Capture.first(:conditions =\> ['criteria = ?', t], :order =\> 'created\_at DESC')

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 11, 2010, 9:15pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/6 "2010-03-11T21:15:01Z")

</div>

> > > Hi guys: This is driving me crazy. Can someone please point me in the right direction:
> > > 
> > > I'm trying to get the last record from my table. Every time I do this, it tells me there is no method, other than id.
> > > 
> > > theconditions = "criteria = '"+t+"'" lastrec = Capture.last(:all, :conditions =\> theconditions)
> > > 
> > > This table has a number of fields in it id, created\_at, tid, text
> > > 
> > > When I call lastrec.id, it works When I call lastrec.created\_at it gives me (undefined method `created\_at' for nil:NilClass)
> > 
> > I feel sure you must be mistaken somehow. Are you saying that if you immediately follow the Capture.last line with id = lastrec.id created\_at = lastrec.created\_at then the first line passes but you get the nil error on the second? I will be surprised if that is the case (I am often surprised mind you).
> > 
> > One thing to try is to run that code in the ruby console. Another is to use ruby-debug to break in after the Capture.last line and examine the data at that point. See the rails guide on debugging if you do not know about ruby-debug ([http://guides.rubyonrails.org/](http://guides.rubyonrails.org/))
> > 
> > Colin
> 
> Hmm, does the id happen to be 4? 😉
> 
> try: \> puts nil.id

I suspect you may well have hit the nail on the head, but why would accessing nil.id in the code not generate the 'Called id for nil' error? It does for me.

Colin

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [March 11, 2010, 10:05pm UTC](https://discuss.rubyonrails.org/t/find-last-errors/50077/7 "2010-03-11T22:05:31Z")

</div>

development mode versus something else? (I think the whiny NilClass is only for development)

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com
