Questions about the initialize method for the ActiveRecord::Relation class.

Asked 2 years ago, Updated 2 years ago, 94 views

This method basically initializes various instance variables, but

after running @values=values while examining self in the process of initializing instance variables I noticed that what self is pointing to has changed.

The result is like this.

I am troubled because I don't know what is going on.
"Why does self point to change when I'm just initializing an instance variable?"

I can't tell you just by writing, so I prepared an example that I can try with my hands.↓↓↓
https://github.com/yukihirop/relation_question

I look forward to hearing from you.

ruby-on-rails ruby

2022-09-30 21:27

2 Answers

What has changed is not "what self refers to" but just the display.

ActiveRecord::Relation performs queries and loads data for the first time when it is needed.When you look at the contents of an object with p or pry, that's also the timing.

I didn't chase the code, but the default <class name:object ID> display because the query cannot be executed without a value in @values, and the query can now be executed after @values.


2022-09-30 21:27

What self points to is not unusual.
When you print self in pry, the display is just different.
If you display self.object_id in pry, you will see that the indication has not changed.

I have not followed the specifications of what is printed in pry, but I am sure you are using the contents of inspect or to_s.If you look at the relationship.rb source, you can see that the inspect method is overriding.The results of this method should probably be different before and after the values assignment.


2022-09-30 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.