diff --git a/README.md b/README.md index d674fe7..4793f5c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ smart people at [Hashrocket](http://hashrocket.com/). ### rails - [Attribute Was](rails/attribute-was.md) +- [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md) - [Show Pending Migrations](rails/show-pending-migrations.md) ### ruby diff --git a/rails/creating-records-of-has-one-associations.md b/rails/creating-records-of-has-one-associations.md new file mode 100644 index 0000000..7e104ba --- /dev/null +++ b/rails/creating-records-of-has-one-associations.md @@ -0,0 +1,27 @@ +# Creating Records of Has_One Associations + +When working with a model, say a User, that has a `has_many` association +with another model, say a Post, you can create a new post for a user like +so: + +```ruby +u1 = User.first +=> # +u1.posts.create(title: "Some Title", content: "...") +=> # +``` + +What about with a `has_one` association? Consider a Customer that has a +`has_one` association with an Account. Rails provides this method for you: + +```ruby +c1.create_account(account_number: 123, ...) +=> # +``` + +Rails also gives you a similar `build` method: + +```ruby +c1.build_account(account_number: 123, ...) +=> # +```