# Model.Save causing nil object error

**URL:** <https://www.ruby-forum.com/t/model-save-causing-nil-object-error/177333>\
**Category:** Rails\
**Created:** [October 15, 2009, 5:39am UTC](https://www.ruby-forum.com/t/model-save-causing-nil-object-error/177333 "2009-10-15T05:39:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [October 15, 2009, 5:39am UTC](https://www.ruby-forum.com/t/model-save-causing-nil-object-error/177333/1 "2009-10-15T05:39:44Z")

</div>

Hello all.

Newbie to rails here and I am having a bit of an issue. I have the  
following models:

class Topic \< ActiveRecord::Base  
validates\_presence\_of :name  
validates\_length\_of :name, :minimum =\> 3  
has\_many :relationship\_topics  
has\_many :relationships, :through =\> :relationship\_topics  
end

class Relationship \< ActiveRecord::Base  
has\_many :relationship\_topics  
has\_many :topics, :through =\> :relationship\_topic  
end

class RelationshipTopic \< ActiveRecord::Base  
validates\_uniqueness\_of :topic, :scope =\> :relationship

belongs\_to :relationship  
belongs\_to :topic  
end

Now, here is example code of me trying to create a new  
RelationshipTopic:

@rel = RelationshipTopic.new(:relationship\_id =\> 1, :topic\_id =\> 1)  
=\> #\<RelationshipTopic id: nil, relationship\_id: 1, topic\_id: 1,  
primary: nil, created\_at: nil, updated\_at: nil\>

> > @rel.save  
> > @rel.save  
> > NoMethodError: You have a nil object when you didn’t expect it!  
> > The error occurred while evaluating nil.text?

Why am I unable to use the .save method when creating  
RelationshipTopics? Creating relationships and topics by itself works,  
but I can’t get the join table to cooperate.

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [October 15, 2009, 7:48am UTC](https://www.ruby-forum.com/t/model-save-causing-nil-object-error/177333/2 "2009-10-15T07:48:24Z")

</div>

Hi Matthew Shapiro

```
  I think you are trying for uniq entry in join table(here 

```

relationship\_topics).That you can do on db level In the migration file  
for this join table you can add after the creation of the table an index  
like

add\_index :relationship\_topics, [:relationship\_id, :topic\_id], :unique  
=\> true

```
Then migrate as usual.Now the model save can be run inside a begin 

```

end block  
and rescue ActiveRecord::StatementInvalid error…You can also remove  
the line

validates\_uniqueness\_of :topic, :scope =\> :relationship  
from above

Sijo

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [October 15, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/model-save-causing-nil-object-error/177333/3 "2009-10-15T14:38:04Z")

</div>

Excellent, that worked!

Thanks,
