背景:
我正在写一个简单的问答网站。与 Stackoverflow 非常相似,在此站点中,用户可以对问题、答案或评论点赞/投票。
问题:
我正在努力编写一个 Ecto 查询,它可以返回用户喜欢/点赞的所有问题、答案或评论。
具体来说,我想编写一个查询:
这个查询似乎可能需要 UNION,据我了解ecto 2.0 尚不支持 UNION。
因此,我想知道是否有人可以向我展示或指出在 Ecto 中解决此类查询的正确方向。感谢您的任何帮助。
以下是相关模型的架构。
...
schema "users" do
...
has_many :answer_upvotes, AnswerUpvote
has_many :comment_upvotes, CommentUpvote
has_many :question_upvotes, QuestionUpvote
many_to_many :upvoted_answers, Answer, join_through: AnswerUpvote
many_to_many :upvoted_comments, Comment, join_through: CommentUpvote
many_to_many :upvoted_questions, Question, join_through: QuestionUpvote
timestamps
end
Run Code Online (Sandbox Code Playgroud)
...
schema "answer_upvotes" do
belongs_to :answer, Answer
belongs_to :user, User
timestamps
end
Run Code Online (Sandbox Code Playgroud)
...
schema "comment_upvotes" do
belongs_to :comment, Comment
belongs_to :user, User
timestamps
end
Run Code Online (Sandbox Code Playgroud)
...
schema "question_upvotes" …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,可以将数字列表转换为连续数字列表
例如,转换数字列表,例如:
[1, 2, 3, 4, 10, 11, 12, 20, 21, 30, 32, 42, 43, 44, 45, 48, 49]
Run Code Online (Sandbox Code Playgroud)
进入连续数字列表,如:
[[1, 2, 3, 4], [10, 11, 12], [20, 21], [30], [32], [42, 43, 44, 45], [48, 49]]
Run Code Online (Sandbox Code Playgroud)
也许我正在推翻这个,但我似乎无法在灵药中找到一个好的解决方案.
欣赏正确方向的任何建议或指示.谢谢!