Examples:
Simple has_many
association
class Blog
has_many :posts
end
class Post
belongs_to :blog
end
blog, post = Blog.new, Post.new
blog.add_post(post)
blog.posts.count
blog.posts.first == post
post.blog == blog
blog.remove_post(post)
blog.posts.count
post.blog = blog
blog.posts.count
blog.posts.first == post
post.blog == blog
post.blog = nil
blog.posts.count
post_2 = Post.new
blog.add_posts([post, post_2])
blog.posts.count
blog.posts == [post, post_2]
blog.remove_posts([post_2, post])
blog.posts.count
Simple has_one
association
class Blog
has_one :posts
end
class Post
belongs_to :blog
end
blog, post = Blog.new, Post.new
blog.post = post
blog.add_post(post)
blog.post == post
post.blog == blog
blog.post = nil
blog.post
post.blog
post.blog = blog
post.add_blog(blog)
post.blog == blog
blog.post == post
post.blog = nil
post.blog
blog.post
Association with readonly belongs_to
(idem for has_one
)
class Blog
has_many :posts
end
class Post
belongs_to :blog, readonly: true
end
blog, post = Blog.new, Post.new
post.blog = blog
Association with explicit class (idem for has_one
)
class Blog
include AIXM::Concerns::Association
has_many :posts, accept: 'Picture'
end
class Picture
include AIXM::Concerns::Association
belongs_to :blog
end
blog, picture = Blog.new, Picture.new
blog.add_post(picture)
blog.posts.first == picture
Polymorphic associator (idem for has_one
)
class Blog
has_many :posts, as: :postable
end
class Feed
has_many :posts, as: :postable
end
class Post
belongs_to :postable
end
blog, feed, post_1, post_2, post_3 = Blog.new, Feed.new, Post.new, Post.new, Post.new
blog.add_post(post_1)
post_1.postable == blog
feed.add_post(post_2)
post_2.postable == feed
post_3.postable = blog
Polymorphic associated (idem for has_one
)
class Blog
include AIXM::Concerns::Association
has_many :items, accept: ['Post', :picture]
end
class Post
include AIXM::Concerns::Association
belongs_to :blog, as: :item
end
class Picture
include AIXM::Concerns::Association
belongs_to :blog, as: :item
end
blog, post, picture = Blog.new, Post.new, Picture.new
blog.add_item(post)
blog.add_item(picture)
blog.items.count
blog.items.first == post
blog.items.last == picture
post.blog == blog
picture.blog == blog
Add method which enriches passed associated object (has_many
only)
class Blog
has_many :posts do |post, related_to: nil|
post.related_to = related_to || @posts.last
end
end
class Post
belongs_to :blog
attr_accessor :related_to
end
blog, post_1, post_2, post_3 = Blog.new, Post.new, Post.new, Post.new
blog.add_post(post_1)
post_1.related_to
blog.add_post(post_2)
post_2.related_to == post_1
blog.add_post(post_3, related_to: post_1)
post_3.related_to == post_1
Add method which builds and yields new associated object (has_many
only)
class Blog
include AIXM::Concerns::Association
has_many :posts do |post, title:| end
end
class Post
include AIXM::Concerns::Association
belongs_to :blog
attr_accessor :title, :text
def initialize(title:)
@title = title
end
end
blog = Blog.new
blog.add_post(title: "title") do |post|
post.text = "text"
end
blog.posts.first.title
blog.posts.first.text