I have a scenario where I have to prevent objects from saving to the database if they have a certain value and I want to come up with the optimal way to achieve that. In my scenario, upon form submit, I will send 20 rows to the update action in the controller. However, I want to discard the 95% of them that have a particular attribute value to save database rows. I''m not sure I want to traverse the hashes: params[:parentobject][:childobject][:grandchildobject][10][attributes][.... within the update action and loop through the objects and delete them if they have a particular value. doing a before_save method within the model being destroyed: self.destroy if self.status = "something" doesn''t seem to be a good option. Any other ideas? -- Posted via http://www.ruby-forum.com/. -- 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.
On 3 April 2011 19:36, John W. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have a scenario where I have to prevent objects from saving to the > database if they have a certain value and I want to come up with the > optimal way to achieve that.You could use a validation. That would be the normal method. Then you can just call save and only the valid ones will actually be saved. Colin -- 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.
Colin Law wrote in post #990706:> On 3 April 2011 19:36, John W. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I have a scenario where I have to prevent objects from saving to the >> database if they have a certain value and I want to come up with the >> optimal way to achieve that. > > You could use a validation. That would be the normal method. Then > you can just call save and only the valid ones will actually be saved. > > ColinValidation failure would make all 20 records and the parent object save fail in totality. I''m trying to have everything save minus the nested objects with a certain field value of "i". All other nested objects with other values save fine. -- Posted via http://www.ruby-forum.com/. -- 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.
accepts_nested_attributes_for takes :reject_if => :method_name (or
Proc.new{|obj| obj.some_value == i})
Garrett Lancaster
> ------------------------------------------------------------------------
>
> John W. <mailto:lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
> April 4, 2011 11:57 AM
>
>
>
> Validation failure would make all 20 records and the parent object save
> fail in totality. I''m trying to have everything save minus the
nested
> objects with a certain field value of "i". All other nested
objects
> with other values save fine.
>
> ------------------------------------------------------------------------
>
> Colin Law <mailto:clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
> April 3, 2011 2:33 PM
>
>
>
> You could use a validation. That would be the normal method. Then
> you can just call save and only the valid ones will actually be saved.
>
> Colin
>
> ------------------------------------------------------------------------
>
> John W. <mailto:lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
> April 3, 2011 1:36 PM
>
>
> I have a scenario where I have to prevent objects from saving to the
> database if they have a certain value and I want to come up with the
> optimal way to achieve that.
>
> In my scenario, upon form submit, I will send 20 rows to the update
> action in the controller. However, I want to discard the 95% of them
> that have a particular attribute value to save database rows.
>
> I''m not sure I want to traverse the hashes:
> params[:parentobject][:childobject][:grandchildobject][10][attributes][....
> within the update action and loop through the objects and delete them
> if they have a particular value.
>
> doing a before_save method within the model being destroyed:
> self.destroy if self.status = "something"
> doesn''t seem to be a good option.
>
> Any other ideas?
>
--
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.
On 4 April 2011 17:57, John W. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #990706: >> On 3 April 2011 19:36, John W. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> I have a scenario where I have to prevent objects from saving to the >>> database if they have a certain value and I want to come up with the >>> optimal way to achieve that. >> >> You could use a validation. That would be the normal method. Then >> you can just call save and only the valid ones will actually be saved. >> >> Colin > > Validation failure would make all 20 records and the parent object save > fail in totality. I''m trying to have everything save minus the nested > objects with a certain field value of "i". All other nested objects > with other values save fine.I am not sure you made it entirely clear that you were using accepts_nested_attributes_for, but perhaps in retrospect it was fairly obvious. In which case you are right and Garrett''s suggestions may be the way to go. Colin -- 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.
Thank you all. The winning line of code is, for future readers''
benefit:
accepts_nested_attributes_for :item_statuses, :reject_if =>
Proc.new{|obj| obj[''status''] == ''i''}
--
Posted via http://www.ruby-forum.com/.
--
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.