# Noob stuck - complex model relationships and one of two foreign keys not getting saved or updated

**URL:** <https://www.ruby-forum.com/t/noob-stuck-complex-model-relationships-and-one-of-two-foreign-keys-not-getting-saved-or-updated/185257>\
**Category:** Rails\
**Created:** [March 16, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/noob-stuck-complex-model-relationships-and-one-of-two-foreign-keys-not-getting-saved-or-updated/185257 "2010-03-16T23:23:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ander31415](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ander31415](https://www.ruby-forum.com/u/ander31415)\
**Post date:** [March 16, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/noob-stuck-complex-model-relationships-and-one-of-two-foreign-keys-not-getting-saved-or-updated/185257/1 "2010-03-16T23:23:07Z")

</div>

Rather than posting my code and asking “what’s wrong”, here’s a  
simpler version: The problem is my C’s are not saving or updating the  
foreign key for B when I create or update a C. The foreign key value  
remains nil.

A has many Bs  
A has many Cs

B has many Cs  
B belongs to A  
The B model contains an A\_id foreign key field

C belongs to A  
C belongs to B  
The C model contains both A\_id and B\_id foreign key fields

So a C can either belong directly to A (with B being null) or it can  
belong to both an A and B. This is not a C belongs to A through B  
relationship - Cs can belong directly to an A. Cs are created using  
current\_A.Bs.new, so they are already automatically scoped to the  
correct A.

When creating or editing a C, a table of possible Bs is created and  
displays correctly and I am able to select one of the possible choices  
or leave it set to the default (‘none selected’ / nil value) choice.  
This all worked fine before I layered on the A model. I was able to  
create Cs that were either free standing or belonged to a B.

Now, the terminal console shows the URL string contains: “B\_id”=\>“15”,  
so it appears that the select function is grabbing the correct id from  
the table for the B choice that was selected. However, when I then  
debug the updated C record, the B\_id value is always nil.

So, why isn’t the B\_id field getting updated in this situation?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ander31415](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ander31415](https://www.ruby-forum.com/u/ander31415)\
**Post date:** [March 16, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/noob-stuck-complex-model-relationships-and-one-of-two-foreign-keys-not-getting-saved-or-updated/185257/2 "2010-03-16T23:23:42Z")

</div>

On Mar 16, 5:47 pm, ander31415 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now, the terminal console shows the URL string contains: “B\_id”=\>“15”,  
> so it appears that the select function is grabbing the correct id from  
> the table for the B choice that was selected. However, when I then  
> debug the updated C record, the B\_id value is always nil.
> 
> So, why isn’t the B\_id field getting updated in this situation?

Hard to say without seeing what you’re actually doing. I know you’ve  
deliberately not posted code, but without code, unless your basic  
premise is fundamentally flawed it’s just guesswork.

Fred

---

<div class="post-metadata">

**Author:** ![ander31415](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ander31415](https://www.ruby-forum.com/u/ander31415)\
**Post date:** [March 17, 2010, 12:46am UTC](https://www.ruby-forum.com/t/noob-stuck-complex-model-relationships-and-one-of-two-foreign-keys-not-getting-saved-or-updated/185257/3 "2010-03-17T00:46:12Z")

</div>

My error (Noob!) - the model for C had set attr\_protected for the B\_id  
field. So Rails was doing exactly as I instructed it. I found this  
by reviewing the development log, which included a warning that B\_id  
was protected against mass updates.

Thanks all.

On Mar 16, 3:13 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Mar 16, 5:47 pm, ander31415 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> \*\*\* CUT \*\*\*
