Hi, I have a list coming in a field like :selected_lists => [1,2,3,4] from a form while storing it in a database i need to join this list into a string so then in my model class, ___________________________________________________ 1) Added a filter before_save :convert_array_to_string 2) Added an attr_accessor :selected_lists ___________________________________________________ def convert_array_to_string self.lists_selected = self.lists_selected.join(",") puts self.lists_selected return true end ---------------------------------------------------------------------------------- so that before saving i call a function to format the array to string and store it using attr_accessor method I printed the "self.lists_selected" and i got "1,2,3,4" as the result I thought this would do my job I am wondering why ''nil'' is stored in the table. Please help me out off this guys. Regards, Vimal Das --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vimal
2009-Mar-19 13:18 UTC
Re: Converting Array to String in a model using before filters
> I have a list coming in a field like :selected_lists => [1,2,3,4] > from a formCorrection :selected_lists => [1,2,3,4] to ---------> :lists_selected => [1,2,3,4] Regards, Vimal Das --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-19 13:56 UTC
Re: Converting Array to String in a model using before filters
On 19 Mar 2009, at 13:16, vimal wrote:> > ---------------------------------------------------------------------------------- > so that before saving i call a function to format the array to > string and store it using attr_accessor method > I printed the "self.lists_selected" and i got "1,2,3,4" as the > result > > I thought this would do my job > I am wondering why ''nil'' is stored in the table. > Please help me out off this guys.Because your attr_accessor overwrote the accessor which saves to a database column with one that saves to an instance variable> > > Regards, > Vimal Das > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vimal
2009-Mar-19 14:00 UTC
Re: Converting Array to String in a model using before filters
So, is there anyother way to carry this out of the box Regards, Vimal Das --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vimal
2009-Mar-20 06:18 UTC
Re: Converting Array to String in a model using before filters
Thanks again Fred to make me try this out... I got it after analysing ur reply further So the trick is not to add a attr_accessor for lists_selected Regards, Vimal Das --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vimal
2009-Mar-20 06:18 UTC
Re: Converting Array to String in a model using before filters
Thanks again Fred to make me try this out... I got it after analysing ur reply further So the trick is not to add a attr_accessor for lists_selected Regards, Vimal Das --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2009-Mar-20 13:22 UTC
Re: Converting Array to String in a model using before filte
vimal wrote:> So, is there anyother way to carry this out of the boxI''m not sure precisely what you''re trying to accomplish here, but from what I can tell it would probably be a lot simpler and more flexible to use the built-in serialization provided by ActiveRecord: ------------------ serialize(attr_name, class_name = Object) If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object, then specify the name of that attribute using this method and it will be handled automatically. The serialization is done through YAML. If class_name is specified, the serialized object must be of that class on retrieval or SerializationTypeMismatch will be raised. Parameters: attr_name - The field name that should be serialized. class_name - Optional, class name that the object type should be equal to. Example # Serialize a preferences attribute class User serialize :preferences end ------------------ Using this technique you can save you Array object to a string field and get back an Array object automatically. All you have to do is add the serialize line in your model as shown above. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---