Extracting information out of a database column

26 views
Skip to first unread message

Joshua Baldock

unread,
Aug 31, 2012, 2:41:41 AM8/31/12
to rubyonra...@googlegroups.com
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/.

Jordon Bedwell

unread,
Aug 31, 2012, 2:58:51 AM8/31/12
to rubyonra...@googlegroups.com
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 

Michael Pavling

unread,
Aug 31, 2012, 3:16:22 AM8/31/12
to rubyonra...@googlegroups.com
On 31 August 2012 07:41, Joshua Baldock <li...@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?

Joshua Baldock

unread,
Aug 31, 2012, 3:18:42 AM8/31/12
to rubyonra...@googlegroups.com
Jordon Bedwell wrote in post #1074013:
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.

Joshua Baldock

unread,
Aug 31, 2012, 3:20:50 AM8/31/12
to rubyonra...@googlegroups.com
Michael Pavling wrote in post #1074016:
Splitting the string and then storing them as additional attributes to
the instance variable that contains the rest of the selected table
values.

Michael Pavling

unread,
Aug 31, 2012, 3:29:44 AM8/31/12
to rubyonra...@googlegroups.com
On 31 August 2012 08:20, Joshua Baldock <li...@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?

Joshua Baldock

unread,
Aug 31, 2012, 3:59:31 AM8/31/12
to rubyonra...@googlegroups.com
Michael Pavling wrote in post #1074020:
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

Michael Pavling

unread,
Aug 31, 2012, 4:13:40 AM8/31/12
to rubyonra...@googlegroups.com
On 31 August 2012 08:59, Joshua Baldock <li...@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

Joshua Baldock

unread,
Sep 2, 2012, 7:04:33 PM9/2/12
to rubyonra...@googlegroups.com
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!
Reply all
Reply to author
Forward
0 new messages