-
Following is a example. abstract class BaseModel < Avram::Model
# ...
def initialize
defaults &.id.desc_order
end
end
class Post < BaseModel
table do
# columns ...
has_many comments : Comment
end
end
class Comment < BaseModel
table do
# columns ...
belongs_to post : Post
end
end When i try get all comments of a post, what i expected is all comments can order by id desc. post = PostQuery.find(env.params.url["post_id"])
post.comments But, when i check log, there is no 2022-08-11T12:20:43.919739Z INFO - avram.query: -- local: {"model" => "Comment", "query" => "SELECT * FROM comments WHERE comments.post_id = $1", "args" => "["1"]", "duration" => "1.22ms"} I try following querying-records#queries-with-defaults, query like this: ReportQuery.new.post_id(post.id) It not work, so, i have to add ReportQuery.new.post_id(post.id).id.desc_order Following is my question:
Thanks EDIT I guess i add code to wrong place .... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yeah, it looks like you added query code to the model. That As for doing the relation, that's not possible at the moment. luckyframework/avram#85 I want to add it in, but I tried, and it's really hard. luckyframework/avram#584 For now, what I do is just create a new method on the model that builds the query how I need. class Post < BaseModel
def blocked_comments : CommentQuery
CommentQuery.new.post_id(self.id).blocked(true)
end
end |
Beta Was this translation helpful? Give feedback.
Yeah, it looks like you added query code to the model. That
initialize
was never being called.As for doing the relation, that's not possible at the moment. luckyframework/avram#85
I want to add it in, but I tried, and it's really hard. luckyframework/avram#584
For now, what I do is just create a new method on the model that builds the query how I need.