<commit_before># paperclip-sample
Rails 4 paperclip usage example has record with multiple image attachments

## The environment

We use [rbenv](https://github.com/sstephenson/rbenv) and current Ruby, 
`ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]`
(on a Mac OS X Yosemite Version 10.10.4, yes, we're spoiled).

We also use [RubyGems](https://rubygems.org) at version 2.4.8 and 
[Bundler](http://bundler.io/v1.9/)
at version 1.9.9 to avoid the dreaded "BUNDLED WITH" virus.
We hope someone soon succeeds in knocking some sense
into the people who added BUNDLED WITH to Bundler and have that removed.
(Several have tried.)
Until then, or until we give up, `gem install bundler -v 1.9.9`

We're used to [RSpec](http://www.rubydoc.info/gems/rspec-rails/frames).
We have nothing to say against MiniTest.
The RSpec people seduced us and that's the world we know.
The Gemfile.lock shows the version in use.
```
rails new -T paperclip-sample
cd paperclip-sample
rails generate rspec:install
```

We're not using RDoc, either.  But perhaps we ought to.

Note that before we write a line of code we have sixty Gemfile
dependencies, plus Rubygems, Bundler, and Ruby itself.
And that's just the ship, riding on the ocean.
We stand on the shoulders of giants.

Many thanks to [Thoughtbot](https://thoughtbot.com/) 
for the [PaperClip](https://github.com/thoughtbot/paperclip) gem,
and their [many other contributions](https://github.com/thoughtbot)
to Rails and open source.


## Procedure

We start by bootstrapping a Rails 4 app according to
[Getting Started With Rails](http://guides.rubyonrails.org/getting_started.html)
Find the version in use documented in the Gemfile.

The tag, '[hello-rails](https://github.com/telegraphy-interactive/paperclip-sample/tree/hello-rails)'
has the basic Rails installation with the hello page.
This is the state of the code base after Section 4 of the getting started guide.

The tag, '[articles-crud](https://github.com/telegraphy-interactive/paperclip-sample/tree/articles-crud)'
has the Articles CRUD application developed in Section 5
of the guide.
We followed it pretty closely, only:
- we used a fancy `:title` content helper.
- we didn't use tables

The tag, '[one-to-many](https://github.com/telegraphy-interactive/paperclip-sample/tree/one-to-many)'
has the comment model developed in Sections 6, 7 and 8.
We put the comment form ahead of the list of comments on the article show view.
As a consequence, the comments list always has the blank comment built,
but not saved, by the comment form.
That looks ugly.  It's especially troublesome when you try interacting
with the delete link for that "phantom" comment.  Ugh.  Not pretty.


### Embedded Form for Aggregated Content

We have an aggregate relation from Article to Comment.  A single article can aggregate many comments.
We have a form on an article that edits a new comment.
In this case, we're happy to add one comment to the article, and have the form fields for that
one comment be present from inception.  We don't need any button or link to add dynamically an
arbitrary number of comments.

We follow the methods taught by:
- [API Doc' for ActiveRecord::NestedAttributes](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)
- [API Doc' for fields_for](http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for)

(These little gems hide away in the RDoc's for Rails.  Shine a little light on them.)

First we add this line into the Article model, `app/models/article.rb`:
```
 accepts_nested_attributes_for :comments, :reject_if => :all_blank, allow_destroy: true
```
The nested attributes method adds dynamic update, insert, and destroy of the dependent comment records,
through params, when updating or creating an article.

For the Comment model, `app/models/comment.rb` we add a validation:
```
  validates_presence_of :article
```
Prevents us saving a comment without an aggregating article.
<commit_msg>improve heading for the comment form change description<commit_after># paperclip-sample
Rails 4 paperclip usage example has record with multiple image attachments

## The environment

We use [rbenv](https://github.com/sstephenson/rbenv) and current Ruby, 
`ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]`
(on a Mac OS X Yosemite Version 10.10.4, yes, we're spoiled).

We also use [RubyGems](https://rubygems.org) at version 2.4.8 and 
[Bundler](http://bundler.io/v1.9/)
at version 1.9.9 to avoid the dreaded "BUNDLED WITH" virus.
We hope someone soon succeeds in knocking some sense
into the people who added BUNDLED WITH to Bundler and have that removed.
(Several have tried.)
Until then, or until we give up, `gem install bundler -v 1.9.9`

We're used to [RSpec](http://www.rubydoc.info/gems/rspec-rails/frames).
We have nothing to say against MiniTest.
The RSpec people seduced us and that's the world we know.
The Gemfile.lock shows the version in use.
```
rails new -T paperclip-sample
cd paperclip-sample
rails generate rspec:install
```

We're not using RDoc, either.  But perhaps we ought to.

Note that before we write a line of code we have sixty Gemfile
dependencies, plus Rubygems, Bundler, and Ruby itself.
And that's just the ship, riding on the ocean.
We stand on the shoulders of giants.

Many thanks to [Thoughtbot](https://thoughtbot.com/) 
for the [PaperClip](https://github.com/thoughtbot/paperclip) gem,
and their [many other contributions](https://github.com/thoughtbot)
to Rails and open source.


## Procedure

We start by bootstrapping a Rails 4 app according to
[Getting Started With Rails](http://guides.rubyonrails.org/getting_started.html)
Find the version in use documented in the Gemfile.

The tag, '[hello-rails](https://github.com/telegraphy-interactive/paperclip-sample/tree/hello-rails)'
has the basic Rails installation with the hello page.
This is the state of the code base after Section 4 of the getting started guide.

The tag, '[articles-crud](https://github.com/telegraphy-interactive/paperclip-sample/tree/articles-crud)'
has the Articles CRUD application developed in Section 5
of the guide.
We followed it pretty closely, only:
- we used a fancy `:title` content helper.
- we didn't use tables

The tag, '[one-to-many](https://github.com/telegraphy-interactive/paperclip-sample/tree/one-to-many)'
has the comment model developed in Sections 6, 7 and 8.
We put the comment form ahead of the list of comments on the article show view.
As a consequence, the comments list always has the blank comment built,
but not saved, by the comment form.
That looks ugly.  It's especially troublesome when you try interacting
with the delete link for that "phantom" comment.  Ugh.  Not pretty.


### Improving the form for adding article comments

We have an aggregate relation from Article to Comment.  A single article can aggregate many comments.
We have a form on an article that edits a new comment.
In this case, we're happy to add one comment to the article, and have the form fields for that
one comment be present from inception.  We don't need any button or link to add dynamically an
arbitrary number of comments.

We follow the methods taught by:
- [API Doc' for ActiveRecord::NestedAttributes](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)
- [API Doc' for fields_for](http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for)

(These little gems hide away in the RDoc's for Rails.  Shine a little light on them.)

First we add this line into the Article model, `app/models/article.rb`:
```
 accepts_nested_attributes_for :comments, :reject_if => :all_blank, allow_destroy: true
```
The nested attributes method adds dynamic update, insert, and destroy of the dependent comment records,
through params, when updating or creating an article.

For the Comment model, `app/models/comment.rb` we add a validation:
```
  validates_presence_of :article
```
Prevents us saving a comment without an aggregating article.

{'fraction_non_alphanumeric': 0.08513816280806571, 'fraction_numerical': 0.01219815782922579, 'mean_word_length': 4.541379310344827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 16, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3559935', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2288, 'n_words': 1033}