Ruby on rails Rails中的逆_属于关联吗?
首先,我知道“逆”是如何在有很多关联的情况下工作的Ruby on rails Rails中的逆_属于关联吗?,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,首先,我知道“逆”是如何在有很多关联的情况下工作的 # relationship without inverse_of user has_many :orders # this will generate two database queries user.orders.first.user Order Load (0.1ms) SELECT `orders`.* FROM `orders` WHERE `orders`.`user_id` = 2 ORDER BY `orders`.`
# relationship without inverse_of
user has_many :orders
# this will generate two database queries
user.orders.first.user
Order Load (0.1ms) SELECT `orders`.* FROM `orders` WHERE `orders`.`user_id` = 2 ORDER BY `orders`.`id` ASC LIMIT 1
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
这里,user和user.order.first.user是两个不同的对象
与之相反的是
# relationship with inverse_of
user has_many :orders, inverse_of: :user
# this will generate just one database query
user.orders.first.user
Order Load (0.1ms) SELECT `orders`.* FROM `orders` WHERE `orders`.`user_id` = 2 ORDER BY `orders`.`id` ASC LIMIT 1
现在,user和user.order.first.user是同一个对象
但是,在这种情况下,什么样的反义词属于联想呢
# relationship with inverse_of in belongs_to association
order belongs_to :user, inverse_of: :orders
order.user.orders
# will generate two database queries
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Order Load (0.1ms) SELECT `orders`.* FROM `orders` WHERE `orders`.`user_id` = 2
在这个关系中,如果我使用逆的或不使用逆的,没有区别。也许我用错了?in的逆u属于联想的意义是什么