Search code examples
htmlruby-on-railsdatabasemodels

Rails: Static html content in database


in my Rails application, a user can refer to a "sample" list of policies and create their own policies based on these samples. I am currently storing the list of "sample" policies and their associated textual HTML descriptions in a SamplePolicy model with a column name Content. Users can read about these sample policies and then create their own versions which are in turn stored in another model called Policy which holds a one-to-many relation with SamplePolicy model. See database structure below:

SamplePolicy
--------------------------------------------------------------
|  ID  |  Name                |  Content | 
--------------------------------------------------------------
|   1  |    Privacy           | <html>Sample..</html>
--------------------------------------------------------------
|   2  |    Copyright policy  |  <html> Sample....</html>
--------------------------------------------------------------

Policy
---------------------------------------------------------------------------------------
|  ID  |  Name                       |        Content               | samplepolicy_id
---------------------------------------------------------------------------------------
|   1  |   Custom Privacy            | <html>My Sample..</html>     |   1
--------------------------------------------------------------------------------------
|   2  |   Custom Copyright policy  |  <html> My Sample....</html> |   2
--------------------------------------------------------------------------------------

Question is:

Is this the best way to structure my models? Since the list of "Sample policies" does not change, I guess I can use "seeds.rb" to populate my application but then I will not be able to refer to these sample policies in my Policy model (See foreign key samplepolicy_id). But then again, I am not sure about storing HTML content in my database.


Solution

  • If you have more than one sample policy I think a seperate model is clean and easy. Storing the HTML is fine. You might use textile so the user can more easily copy and edit the sample into their own policy.

    If you do store HTML just use html_safe when rendering.