--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
> student = Student.first
Student Load (0.5ms) SELECT "students".* FROM "students" ORDER BY "students"."id" ASC LIMIT 1
> student.user
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."profile_id" = $1 AND "users"."profile_type" = $2 LIMIT 1 [["profile_id", 1], ["profile_type", "Student"]]
> student.user(true)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."profile_id" = $1 AND "users"."profile_type" = $2 LIMIT 1 [["profile_id", 1], ["profile_type", "Student"]]
> student.user.reload
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
>
I agree that having a single API is nice. However, I feel like it would be confusing if user want to reload the has_one
association, as they have to reload the original record instead of just passing true
.
Consider this scenario:
# User has_one :profile, Profile belongs_to :user
> user = User.first
> user.profile #=> #<Profile id: 1, user_id: 1>
> Profile.find(1).update_attrbutes!(user_id: 2)
> Profile.find(1) #=> #<Profile id: 1, user_id: 2>
> user.profile #=> #<Profile id: 1, user_id: 1>
> user.profile.reload #=> #<Profile id: 1, user_id: 2> # << ?!
> user.profile(true) #=> nil
You can see that by removing the support for passing an argument to force reload makes it more confusing as you get a stale record. The workaround is to call user.reload.profile
instead.
If you think that workaround is good enough, I don’t mind submit another PR to remove the support to simplify the API, though.
-Prem
On July 15, 2015 at 9:08:04 AM, DHH (da...@loudthinking.com) wrote:
I'd prefer to see us move to a single API, and IMO the trade-offs for #reload alone fits the bill. There's no contract saying that a singular object from has_one/belongs_to and a collection like has_many has to behave the same. In fact, I think it'd be strange to think that it should. A single string and an array of strings do not behave the same.So project.documents.reload.first makes perfect sense to me as reloading the documents collection, then grabbing the first one. projects.owner.reload.name makes sense to me as reloading the owner record itself, then fetching the name (if we don't already return self from Record#reload, we should).
DHH wrote:
> I'm content with that work-around. I think it's a very rare situation. Let's make common things easy and uncommon things possible.
Alright, I'm sold. Let me submit another PR to deprecate that then.
Thank you for your thoughts, everyone.
-Prem
--
On 16/07/2015, at 01:08 , DHH <da...@loudthinking.com> wrote:
I'd prefer to see us move to a single API, and IMO the trade-offs for #reload alone fits the bill. There's no contract saying that a singular object from has_one/belongs_to and a collection like has_many has to behave the same. In fact, I think it'd be strange to think that it should. A single string and an array of strings do not behave the same.So project.documents.reload.first makes perfect sense to me as reloading the documents collection, then grabbing the first one. projects.owner.reload.name makes sense to me as reloading the owner record itself, then fetching the name (if we don't already return self from Record#reload, we should).