# Belongs\_to not working as expected

**URL:** <https://www.ruby-forum.com/t/belongs-to-not-working-as-expected/166395>\
**Category:** Rails\
**Created:** [April 24, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/belongs-to-not-working-as-expected/166395 "2009-04-24T19:28:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![uberlordchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/bcef8e/32.png) [@uberlordchris](https://www.ruby-forum.com/u/uberlordchris)\
**Post date:** [April 24, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/belongs-to-not-working-as-expected/166395/1 "2009-04-24T19:28:16Z")

</div>

I’m using ActiveRecord 2.3.2. I have a class with a belongs\_to  
association like this:

class Foo \< ActiveRecord::Base  
belongs\_to :bar  
end

It is valid for bar to be nil (thus, in the database, foos.bar\_id can  
be null). However, if I execute the following I get an error:

foo = Foo.new  
foo.bar # =\> nil  
foo.save!  
foo.bar # expect nil but get error

The full error message is, “ActiveRecord::RecordNotFound (Couldn’t  
find Bar without an ID)”. Tracing the code I see that  
ActiveRecord::Base.find\_from\_ids(ids, options) is being called by  
passing an array containing a single, nil, element for the ids  
argument. Here is the method:

def find\_from\_ids(ids, options)  
expects\_array = ids.first.kind\_of?(Array)  
return ids.first if expects\_array && ids.first.empty?

```
ids = ids.flatten.compact.uniq

case ids.size
  when 0
    raise RecordNotFound, "Couldn't find #{name} without an ID"
  when 1
    result = find_one(ids.first, options)
    expects_array ? [result] : result
  else
    find_some(ids, options)
end

```

end

Without delving further to assess other consequences, it seems to me  
that either nil should be returned when ids.size == 0 or the line “ids  
= ids.flatten.compact.uniq” should be replaced with:

ids = ids.flatten.uniq  
return nil if ids.length == 1 && ids.first.nil?  
ids.compact!

Any thoughts gratefully received!

Thanks,

Chris.

---

<div class="post-metadata">

**Author:** ![uberlordchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/bcef8e/32.png) [@uberlordchris](https://www.ruby-forum.com/u/uberlordchris)\
**Post date:** [April 24, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/belongs-to-not-working-as-expected/166395/2 "2009-04-24T21:06:17Z")

</div>

uberlordchris wrote:

> I’m using ActiveRecord 2.3.2. I have a class with a belongs\_to  
> association like this:
> 
> class Foo \< ActiveRecord::Base  
> belongs\_to :bar  
> end
> 
> It is valid for bar to be nil (thus, in the database, foos.bar\_id can  
> be null). However, if I execute the following I get an error:
> 
> foo = Foo.new  
> foo.bar # =\> nil  
> foo.save!  
> foo.bar # expect nil but get error

I just tried this myself with Rails 2.3.2 and it worked as expected so  
I’m not sure what wrong.

What does your Foo table look like?

Mine is:

## foos

|id|bar\_id|name|created\_at|updated\_at|
