# Belongs\_to sometimes prevents the object from being saved

**URL:** <https://www.ruby-forum.com/t/belongs-to-sometimes-prevents-the-object-from-being-saved/127905>\
**Category:** Rails\
**Created:** [February 2, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/belongs-to-sometimes-prevents-the-object-from-being-saved/127905 "2008-02-02T13:01:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![truetype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@truetype](https://www.ruby-forum.com/u/truetype)\
**Post date:** [February 2, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/belongs-to-sometimes-prevents-the-object-from-being-saved/127905/1 "2008-02-02T13:01:30Z")

</div>

Seems to be a bug. I can’t dig into the core of it (since too many  
models), but here is what I know.

I have a model user, which has primary key :id.  
I also have a model of action. Two users interact with each other  
through the model of action. Say, one user sends a comment to the  
other. So, action model has primary key :id, and also :sender\_id  
and :receiver\_id, which are ids of sender and receiver,  
correspondingly. There is also a concept of rating. Receiver can rate  
the sender for his comment. That is, rating doesn’t have a primary  
key, yet it has :user\_id (which is the id of receiver) and :action\_id.

The model for ACTION has:  
belongs\_to sender, :class\_name =\> ‘User’, :foreign\_key =\> :sender\_id  
belongs\_to receiver, :class\_name =\> ‘User’, :foreign\_key  
=\> :receiver\_id

The model for USER has:  
has\_many :sent\_actions, :class\_name =\> ‘Action’, :foreign\_key  
=\> :sender\_id  
has\_many :received\_actions, :class\_name =\> ‘Action’, :foreign\_key  
=\> :receiver\_id

The model for RATING has:  
belongs\_to :user  
belongs\_to :action

Now the funniest part. When I try to save new rating, it’s not saved,  
while rating has belongs\_to :action. If this line is commented, then  
everything is ok - rating is saved and rating.action\_id is in place in  
the database.

I dug into into save! of the ActiveRecord class and put a “raise  
‘testing…’” there instead of its usual code. Now, if  
belongs\_to :action is inside the model, then save! doesn’t fire off  
with an error, which means that rails doesn’t even go inside save!. If  
belongs\_to :action is commented, then ‘testing…’ appear on the  
screen, while trying to save a rating.

Did anyone bump into anything similar? How to override? Or it’s a bug  
and I need to forget about it and find working workaround? 🙂

Thanks.

---

<div class="post-metadata">

**Author:** ![truetype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@truetype](https://www.ruby-forum.com/u/truetype)\
**Post date:** [February 3, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/belongs-to-sometimes-prevents-the-object-from-being-saved/127905/2 "2008-02-03T14:34:46Z")

</div>

truetype wrote:

> ‘testing…’" there instead of its usual code. Now, if  
> belongs\_to :action is inside the model, then save! doesn’t fire off  
> with an error, which means that rails doesn’t even go inside save!. If  
> belongs\_to :action is commented, then ‘testing…’ appear on the  
> screen, while trying to save a rating.

Are you saying you’re calling save! on the rating but the save! method  
is never called? Is its associated action object new? Is the action  
object valid? You can try adding validates\_associated :action to the  
Rating model. If that doesn’t work, please post the relevant section  
of your code.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![truetype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@truetype](https://www.ruby-forum.com/u/truetype)\
**Post date:** [February 3, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/belongs-to-sometimes-prevents-the-object-from-being-saved/127905/3 "2008-02-03T15:02:27Z")

</div>

The name ‘action’ has been reported to cause issues. Try using a  
different name for the model
