Personally, I think this a case where additional modeling of the domain would be useful. A string of text is simply too generic to encapsulate all of the data about itself. For example, in this case, you are struggling to store information about specific parts of the string in the string itself.
Instead, I suggest the following modeling of the problem domain:
Note: I am assuming that you already have a model called Article
which currently has a field/property called text
.
class Article < ActiveRecord::Base
has_one :passage
end
and
class Passage < ActiveRecord::Base
belongs_to :article
has_many :highlighted sections
# sample database fields
# text -- the entire text
end
and
class HighlightedSection < ActiveRecord::Base
belongs_to :passage
# sample database fields
# int start -- the (string) index of the start of the passage
# int end -- the (string) index of the end of the passage
end
Together, this model allows you to store sections. You could also expand the model to ensure that passages do not overlap, etc.
Good luck with your application!