|
Testing the models
Now that we have created the models, let's test it out quickly:- irb> a = Author.create(name: "Charles Dickens")
- => #<Author _id: 5143678345db7ca255000001, name: "Charles Dickens">
- irb> a.create_address(street: "Picadilly Circus", city: "London", country: "UK")
- => #<Address _id: 514367f445db7ca255000003, street: "Picadilly Circus", city: "London", state: nil, zipcode: nil, country: "UK">
复制代码 As we can see, this creates an Author object and its corresponding Address object. Mongoid includes ActiveModel and you may notice the similarity in these methods if you have used ActiveRecord.
Tip
We have used create_address because an author has only one embedded address. If, an author had multiple addresses, we would have used a.addresses.create. |
|