locomotivecms/models — pull request #23 by joel (2014-06-09 16:35:23+00:00)
Title: Save reference of parent object in child object and retrieve it

```
class Article
include Locomotive::Entity
attributes :title, :content, :author, :comments
end

class Comment
include Locomotive::Entity
attributes :title, :content, :article_id
end

require_relative 'lib/locomotive/adapters/memory_adapter'
adapter = Locomotive::Adapters::MemoryAdapter

mapper = Locomotive::Mapper.new(adapter) do
collection :articles do
entity Locomotive::Example::Article
repository Locomotive::Example::ArticlesRepository

attribute :title, localized: true
attribute :content, klass: String

attribute :comments, association: :comments
end

collection :comments do
entity Locomotive::Example::Comment
repository Locomotive::Example::CommentsRepository

attribute :title
attribute :content
attribute :article_id
end
end

Comment.new( { title: 'awesome comment', content: 'Lorem ipsum dolor sit amet, ...' })
Article.new(title: { en:'My title' }, content: 'The article content', comments: [])

articles_repository.create article
article.id.should_not be_nil

article.comments << comment
articles_repository.update article

comment.article_id.should eq article.id

article_double = articles_repository.find(article.id)
comment = article_double.comments.first

comment.article_id.should eq article.id
```