# How do you update one object of a has\_many :through relationship

**URL:** <https://www.ruby-forum.com/t/how-do-you-update-one-object-of-a-has-many-through-relationship/190236>\
**Category:** Rails\
**Created:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-update-one-object-of-a-has-many-through-relationship/190236 "2010-06-17T16:22:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-update-one-object-of-a-has-many-through-relationship/190236/1 "2010-06-17T16:22:28Z")

</div>

Hi guys.

How do you update nested attributes for one object of a  
has\_many :through relationship? So I have an entity model and this  
model can have many fact\_values. The difference between fact and  
fact\_value is that the latter has an extra value attribute. This is  
because facts can be common among many entities, but each entity can  
have its own unique value for a specific fact. So my models are the  
following:

class Entity  
has\_many :fact\_values  
has\_many :facts, :through =\> :fact\_values  
end

class FactValue  
belongs\_to :entity  
belongs\_to :fact  
end

class Fact  
has\_many :fact\_values  
has\_many :entities, :through =\> :fact\_values  
end

Now I’ve specified that Entity  
accepts\_nested\_attributes\_for :fact\_values. But I want it to accept  
nested attributes for a single (pre-defined) fact\_value and update  
only that fact\_value. The way I’m going about it right now is to have  
a virtual attribute in my Entity model representing that one  
fact\_value and update that fact\_value in the entity controller. I’m  
wondering if there is any automatic way to accomplish what I’m trying  
to do?

Thanks!

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-update-one-object-of-a-has-many-through-relationship/190236/2 "2010-06-17T16:22:35Z")

</div>

On Mon, Jun 14, 2010 at 1:55 AM, Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Entity  
> has\_many :fact\_values  
> to do?  
> Perhaps something along the lines of

class Entity

has\_many :fact\_values  
has\_many :facts, :through =\> :fact\_values  
belongs\_to :anointed\_fact\_value

accepts\_nested\_attributes\_for :fact\_values, :reject\_if =\>  
:not\_updatable\_fact\_value

def not\_updatable\_fact\_value(attrs)  
attrs[:id] != anointed\_fact\_value\_id  
end  
end

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-update-one-object-of-a-has-many-through-relationship/190236/3 "2010-06-17T16:22:36Z")

</div>

On Jun 14, 7:55 am, Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want it to accept nested attributes for a single (pre-defined)  
> fact\_value and update only that fact\_value

I’m not sure exactly what the problem is. If you only want to update  
one object, you only put that object in the fields\_for(:fact\_values)  
part of the form.  
If you’re worried about the wrong object being created/updated through  
accepts\_nested\_attributes, then the :reject\_if can enable you to add  
validation.

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-update-one-object-of-a-has-many-through-relationship/190236/4 "2010-06-17T16:22:49Z")

</div>

I’m using formtastic, I see that it seems simple enough with the  
standard  
rails forms. I suppose I can ask on a formtastic gorup to see what they  
say.

Thanks
