Ali
2010-Jun-14 05:55 UTC
How do you update one object of a has_many :through relationship
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! -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz
2010-Jun-14 13:18 UTC
Re: How do you update one object of a has_many :through relationship
On Jun 14, 7:55 am, Ali <ali.akhtarz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want it to accept nested attributes for a single (pre-defined) > fact_value and update only that fact_valueI''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. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rick DeNatale
2010-Jun-14 13:20 UTC
Re: How do you update one object of a has_many :through relationship
On Mon, Jun 14, 2010 at 1:55 AM, Ali <ali.akhtarzada-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ali Akhtarzada
2010-Jun-15 10:53 UTC
Re: How do you update one object of a has_many :through relationship
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 On Tue, Jun 15, 2010 at 1:20 AM, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> On Mon, Jun 14, 2010 at 1:55 AM, Ali <ali.akhtarzada-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.