views:

15

answers:

1

I am a Rails novice and I'm creating an application that I'd love some help on. The app is for social reading. It enables a user to highlight a passage from a text then have that passage saved and bolded into the view for later reading by anyone else.

I have no problem creating an if statement on whether a certain string of text is in a passage but how do I add a class to that string and have a group of these passages absorbed back into the view. Any strategic advice would be greatly appreciated.

+1  A: 

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!

nickname