-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change .all and has_many to return *Collection object #176
Change .all and has_many to return *Collection object #176
Conversation
Just looking at the doc you have here in the pull, I think it’s a good idea. Granite is currently pretty chatty and I’d like to see that cleaned up however possible. I haven’t looked at the code here yet, but I suspect I’ve implemented something vaguely similar in the query builder (so I’m obviously onboard with the idea). Is the idea here to prevent queries from being run on variables that are never used? |
enrollment3.student = student | ||
enrollment3.save | ||
|
||
klasses = student.klasss.all("AND klasss.name = ? ORDER BY klasss.id DESC", ["Test class X"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This syntax is an improvement for sure
@robacarp I want to improve way working with relations. Since I needed something in the middle to provide methods like I believe that preventing queries from unused variables is a very good thing. I've seen a lot of code written with passing down collection through few methods to not be used because of one condition at the end. Since these collections forwards missing to the array the only breaking change there is returned type. I believe this is a small problem compared to possibilities it gives and "free" optimization |
@faustinoaq we should label this with a @Adam-Stomski this is nice addition 👍 |
2658262
to
8ffc7fd
Compare
enrollment1.student = student | ||
enrollment1.klass = klass1 | ||
enrollment1.save | ||
klass = teacher.klasss.find_by!(:name, "Test class with different name").not_nil! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the not_nil!
since you are using the find_by!
enrollment3.klass = klass2 | ||
enrollment3.student = unrelated_student | ||
enrollment3.save | ||
klass = teacher.klasss.find!(klass1.id).not_nil! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
enrollment3.student = student | ||
enrollment3.save | ||
|
||
klass = student.klasss.find_by!(:name, "Test class with different name").not_nil! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
enrollment3.student = student | ||
enrollment3.save | ||
|
||
klass = student.klasss.find!(klass1.id).not_nil! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent addition. Thanks! 💯
@robacarp we will need to update the query dsl pr
@robacarp need another approval when you get a chance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for removing the redundant .not_nil!
My proposal for lazy loaded collections.
Now using
.all
method queries database immediately, after this change it would return aGranite::ORM::Collection(Model)
which forwards missing methods to the array of records.Now using
has_many
association methods, loads all related records from the database. After this change, it would return aGranite::ORM::AssociationCollection(Owner, Target)
. This allows easy extensions on associations (for example #121 should be easy to implement asassociation.delete_all
). Includedfind_by
andall
as an example for this.new methods
I didn't want any new query syntax, just reuse what Granite already has, making associations less dumb, and prevent unnecessary db queries. Also didn't add any docs for now, would like to hear you opinion on this first :)