On Mon, Jun 14, 2010 at 1:55 AM, Ali <ali.akhtarz...@gmail.com> wrote:
> 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?

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/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to