Skip to content
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

inverse_of not working with has_many #3223

Closed
rafamvc opened this issue Oct 4, 2011 · 7 comments
Closed

inverse_of not working with has_many #3223

rafamvc opened this issue Oct 4, 2011 · 7 comments

Comments

@rafamvc
Copy link

rafamvc commented Oct 4, 2011

When using the inverse_of with a has_one it won't instantiate another object, but it will instantiate a new object when using has_many.

ruby-1.9.2-p290 :001 > e = Employee.create(name: 'rafael', active: false)
ruby-1.9.2-p290 :002 > b = Badge.create(number: 1, employee: e)
ruby-1.9.2-p290 :003 > a = Address.create(street: "123 Market St", city: "San Diego", employee: e)
ruby-1.9.2-p290 :004 > e = Employee.first
  Employee Load (0.2ms)  SELECT "employees".* FROM "employees" LIMIT 1
 => #<Employee id: 1, name: "rafael", active: false, created_at: "2011-10-04 17:09:25", updated_at: "2011-10-04 17:09:25"> 
ruby-1.9.2-p290 :002 > e.is_active?
 => false 
ruby-1.9.2-p290 :003 > e.object_id
 => 2182895380 
ruby-1.9.2-p290 :004 > e.badge.employee.is_active?
  Badge Load (17.6ms)  SELECT "badges".* FROM "badges" WHERE "badges"."employee_id" = 1 LIMIT 1
 => false 
ruby-1.9.2-p290 :005 > e.badge.employee.object_id
 => 2182895380 
ruby-1.9.2-p290 :006 > e.addresses.first.employee.is_active?
  Address Load (0.2ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."employee_id" = 1 LIMIT 1
  Employee Load (0.3ms)  SELECT "employees".* FROM "employees" WHERE "employees"."id" = 1 LIMIT 1
 => false 
ruby-1.9.2-p290 :007 > e.addresses.first.employee.object_id
  Address Load (0.3ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."employee_id" = 1 LIMIT 1
  Employee Load (0.2ms)  SELECT "employees".* FROM "employees" WHERE "employees"."id" = 1 LIMIT 1
 => 2181302220 
ruby-1.9.2-p290 :008 >

Those are the classes that I used to test this, in a brand new rails app:
https://gist.github.com/121fb94c31494ce8c90d

@ghost ghost assigned jonleighton Oct 4, 2011
@korny
Copy link
Contributor

korny commented Feb 19, 2012

Does it work when you use addresses[0] instead of addresses.first?

I think the implementation of :inverse_of doesn't work for the code example given in the documentation, since has_many's #first method always invokes a new query that is not cached.

@isaacsanders
Copy link
Contributor

Is this still an issue?

@gamov
Copy link

gamov commented Jun 6, 2012

For me yes (3.0.12), https://gist.github.com/445969

@holli
Copy link
Contributor

holli commented Aug 20, 2012

@korny thanks for the adresses[0] hint. I spent quite a while wondering why my code wasn't using the inverse_of info. Inverse_of wont work if you use any query method to fetch any object?

In a sense this is bit annoying.

current_employee = Employee.find(session[:employee_id])

editable_address = employee.addresses.find_by_id(params[:id))

# now I have lots of things inside address-class that use employee to check variables
# I have to manually use following line to avoid extra sql queries
editable_address.employee = current_employee

# Rest of the code for requests, code has something like following quite often
editable_address.employee

Is there a way to do this cleanly or do I really need to have the editable_address.employee = current_employee to avoid using extra queries in every request?

Btw I updated the railsdocs according to the info in this issue: https://github.com/lifo/docrails/commit/121cf33eac5467ec01035af40f92ea4480ca4045

@brainopia
Copy link
Contributor

@lifo Actually, has_many with inverse_of option supports #first/#last since #7377 (it was merged a couple of days ago). So doc patch is not needed.

@rafaelfranca
Copy link
Member

I reverted that commit in docrails

@carlosantoniodasilva
Copy link
Member

This has been fixed in master and 3-2-stable with 7377 patch, let us know if you still have issues with that, thanks!

sgerrand pushed a commit to sgerrand/rails that referenced this issue Nov 2, 2013
Fixed bug in the example code. A bit related to rails issue in
 rails#3223
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants