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?
on 2012-08-31 08:41
on 2012-08-31 08:59
On Friday, 31 August 2012 01:42:10 UTC-5, Ruby-Forum.com User wrote: > > 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-...
on 2012-08-31 09:17
On 31 August 2012 07:41, Joshua Baldock <lists@ruby-forum.com> 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?
on 2012-08-31 09:18
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-... 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.
on 2012-08-31 09:20
Michael Pavling wrote in post #1074016: > On 31 August 2012 07:41, Joshua Baldock <lists@ruby-forum.com> 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.
on 2012-08-31 09:31
On 31 August 2012 08:20, Joshua Baldock <lists@ruby-forum.com> 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?
on 2012-08-31 09:59
Michael Pavling wrote in post #1074020: > On 31 August 2012 08:20, Joshua Baldock <lists@ruby-forum.com> 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
on 2012-08-31 10:15
On 31 August 2012 08:59, Joshua Baldock <lists@ruby-forum.com> 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
on 2012-09-03 01:04
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!
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.