README.md in sprig-0.1.6 vs README.md in sprig-0.1.7

- old
+ new

@@ -30,11 +30,10 @@ Seed files are unique to the environment in which your Rails application is running. Within `db/seeds` create an environment-specific directory (i.e. `/development` for your 'development' environment). Todo: [Support for shared seed files] - ##Seed files Hang your seed definitions on a `records` key for *yaml* and *json* files. Examples: @@ -72,10 +71,12 @@ } ``` Each seed record needs a `sprig_id` defined that must be *unique across all seed files per class*. It can be an integer, string, whatever you prefer; as long as it is unique, Sprig can sort your seeds for insertion and detect any cyclic relationships. +### Relationships + Create relationships between seed records with the `sprig_record` helper: ```yaml # comments.yml @@ -83,10 +84,32 @@ - sprig_id: 1 post_id: "<%= sprig_record(Post, 1).id %>" body: "Yaml Comment body" ``` +#### Has and Belongs to Many +For `has_and_belongs_to_many` (HABTM) relationships, you may define relation ids in array format. So if `Post` `has_and_belongs_to_many :tags`, you could write: +```yaml +#posts.yml + +records: + - sprig_id: 1 + title: 'All About Brains' + content: 'Lorem ipsum...' + tag_ids: + - '<%= sprig_record(Tag, 1).id %>' + - '<%= sprig_record(Tag, 2).id %>' +``` +```yaml +#tags.yml + +records: + - sprig_id: 1 + name: 'Biology' + - sprig_id: 2 + name: 'Neuroscience' +``` **Note: For namespaced or STI classes, you'll need to include the namespace with the class name in the seed file name. For example `Users::HeadManager` would need to be `users_head_managers.yml`** ### Special Options These are provided in a `options:` key for *yaml* and *json* files. @@ -122,10 +145,12 @@ ##Custom Sources and Parsers If all your data is in `.wat` files, fear not. You can tell Sprig where to look for your data, and point it toward a custom parser class for turning your data into records. The example below tells Sprig to read `User` seed data from a Google Spreadsheet, and parse it accordingly. ```ruby +require 'open-uri' + fanciness = { :class => User, :source => open('https://spreadsheets.google.com/feeds/list/somerandomtoken/1/public/values?alt=json'), :parser => Sprig::Data::Parser::GoogleSpreadsheetJson } @@ -148,10 +173,10 @@ ``` ## Populate Seed Files from Database Want to create Sprig seed files from the records in your database? Well, -[Sprig::Reap](http://www.rubygems.org/sprig-reap) can create them for you! Check out the gem's +[Sprig::Reap](https://rubygems.org/gems/sprig-reap) can create them for you! Check out the gem's [README](https://github.com/vigetlabs/sprig-reap#sprigreap) for installation instructions and details on usage. ## License