Hi All, I''m new to rails and am trying to tackle a problem that I can''t seem to find the right solution for. I have an existing database that im building a rails app on top of. One of my columns ''message'' is a long string that I would like to be able to query and split into various attributes to then be able to display each of those attributes in a view. I''m trying to implement this into the controller, but don''t seem to be getting any where. Can you please let me know how I can achieve this within rails? -- 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 https://groups.google.com/groups/opt_out.
On Friday, 31 August 2012 01:42:10 UTC-5, Ruby-Forum.com User wrote:> > Hi All, > > I''m new to rails and am trying to tackle a problem that I can''t seem to > find the right solution for. > > I have an existing database that im building a rails app on top of. One > of my columns ''message'' is a long string that I would like to be able to > query and split into various attributes to then be able to display each > of those attributes in a view. > > I''m trying to implement this into the controller, but don''t seem to be > getting any where. > > Can you please let me know how I can achieve this within rails? >I don''t quite understand exactly what you mean but it sounds to me like you want a custom serializer. http://stackoverflow.com/questions/4472479/custom-serialization-for-fields-in-rails -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/6lDoxISEdbUJ. For more options, visit https://groups.google.com/groups/opt_out.
On 31 August 2012 07:41, Joshua Baldock <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have an existing database that im building a rails app on top of. One > of my columns ''message'' is a long string that I would like to be able to > query and split into various attributes to then be able to display each > of those attributes in a view. > > I''m trying to implement this into the controller, but don''t seem to be > getting any where.Which bit are you having trouble with? Accessing the legacy table? Splitting the string? Displaying it in a view? -- 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 https://groups.google.com/groups/opt_out.
Jordon Bedwell wrote in post #1074013:> On Friday, 31 August 2012 01:42:10 UTC-5, Ruby-Forum.com User wrote: > > I don''t quite understand exactly what you mean but it sounds to me like > you > want a custom serializer. >http://stackoverflow.com/questions/4472479/custom-serialization-for-fields-in-rails Maybe some example information may help :) The table in my database has a few different collumns, one of which is "message". In this column and example message looks like this: "The [user] is logged in via [hostname]" When i query the database and get back each of these matching rows, i would like to be able to split [user] and [hostname] into a user and host attributes so that i can call those attributes on my view, rather then have logic in my view that takes the message attribute and breaks it up for each row. Please let me know if im still not making sense. Thanks for your help. -- 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 https://groups.google.com/groups/opt_out.
Michael Pavling wrote in post #1074016:> On 31 August 2012 07:41, Joshua Baldock <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I have an existing database that im building a rails app on top of. One >> of my columns ''message'' is a long string that I would like to be able to >> query and split into various attributes to then be able to display each >> of those attributes in a view. >> >> I''m trying to implement this into the controller, but don''t seem to be >> getting any where. > > Which bit are you having trouble with? Accessing the legacy table? > Splitting the string? Displaying it in a view?Splitting the string and then storing them as additional attributes to the instance variable that contains the rest of the selected table values. -- 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 https://groups.google.com/groups/opt_out.
Michael Pavling
2012-Aug-31 07:29 UTC
Re: Re: Extracting information out of a database column
On 31 August 2012 08:20, Joshua Baldock <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Splitting the string and then storing them as additional attributes to > the instance variable that contains the rest of the selected table > values.Do you have an example of the string; the pattern that similar instances would follow, and the components you need it split into? -- 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 https://groups.google.com/groups/opt_out.
Joshua Baldock
2012-Aug-31 07:59 UTC
Re: Re: Extracting information out of a database column
Michael Pavling wrote in post #1074020:> On 31 August 2012 08:20, Joshua Baldock <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Splitting the string and then storing them as additional attributes to >> the instance variable that contains the rest of the selected table >> values. > > Do you have an example of the string; the pattern that similar > instances would follow, and the components you need it split into?Thanks Michael, Message column and example message looks like this: "The [user] is logged in via [hostname]" When i query the database and get back each of these matching rows, i would like to be able to split [user] and [hostname] into a user and host attributes so that i can call those attributes on my view -- 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 https://groups.google.com/groups/opt_out.
Michael Pavling
2012-Aug-31 08:13 UTC
Re: Re: Re: Extracting information out of a database column
On 31 August 2012 08:59, Joshua Baldock <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Message column and example message looks like this: > > "The [user] is logged in via [hostname]"I assume the square brackets are not in the string, so that a real string might look like : "The administrator is logged in via michael-desktop" ? If so, you''ll probably want to play with some string matching methods. http://www.ruby-doc.org/core-1.9.3/String.html "match" is a good place to start. class MyModel < AR::Base # my model has a big string field called "note_details", from # which I want to extract the username and hostname values def username note_details.match(/The (\S*) is logged in via (\S*)/)[1] end def hostname note_details.match(/The (\S*) is logged in via (\S*)/)[2] end end in the view you can access @my_model.username and @my_model.hostname like any other attributes. If you have any issue with performance, you could memoize the results, so you only run the .match once. HTH -- 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 https://groups.google.com/groups/opt_out.
Joshua Baldock
2012-Sep-02 23:04 UTC
Re: Re: Re: Extracting information out of a database column
Michael Pavling wrote in post #1074025:> "match" is a good place to start. > > class MyModel < AR::Base > # my model has a big string field called "note_details", from > # which I want to extract the username and hostname values > > def username > note_details.match(/The (\S*) is logged in via (\S*)/)[1] > end > > def hostname > note_details.match(/The (\S*) is logged in via (\S*)/)[2] > end > end >Thanks Michael, that worked a treat! -- 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 https://groups.google.com/groups/opt_out.