In Rails, wenn ich die folgende Konfiguration haben:Mischen Scopes und Verbände in Phoenix/Ecto
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
def self.approved
where(approved: true)
end
end
Dann kann ich etwas tun:
post = Post.find(100)
comments = post.comments.approved
schnell die genehmigten alle Kommentare zu erhalten, für das gegebene Post
.
Wie kann ich etwas ähnliches in Ecto tun?
defmodule MyApp.Post do
use Ecto.Model
schema "posts" do
#columns omitted
has_many :comments, MyApp.Comment
end
end
defmodule MyApp.Comment do
use Ecto.Model
schema "comments" do
#columns omitted
belongs_to :post, MyApp.Post
end
end
Ich habe die post
mit comments
vorbelastet:
post = MyApp.Post
|> MyApp.Repo.get(100)
|> MyApp.Repo.preload(:comments)
Ich bin nicht einmal sicher, wo mit dem approved
Umfang in MyApp.Comment
zu starten.
Dieser Blog-Eintrag Sie in die richtige Richtung zeigen kann: http://blog.drewolson.org/composable-queries-ecto/ – Gazler