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?
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.