The Somewhat-Extremely-Helpful inverse_of Option

By: Johnathon Wright on: October 04, 2012

Let's say you have the following association set up:

class Route < ActiveRecord::Base hasmany :appointments, :inverseof => :route # long live the hash rocket! end

class Appointment < ActiveRecord::Base belongsto :route, :inverseof => :appointments


the 'inverse-of' option is supposed to prevent unneeded queries.

If you ask a route for one of its appointments, and then ask that appointment for its route, there's no need to issue a query... the route is already in memory. Without inverse_of, though, it will go to the db. Say it with me boys and girls, "Lack of referential integrity."

r = Route.find 10

Route Load (0.2ms) SELECT * FROM routes WHERE ( = 10)

a = r.appointments.first

Appointment Load (1.2ms) SELECT * FROM appointments WHERE (appointments.route_id = 10)


Route Load (0.2ms) SELECT * FROM routes WHERE ( = 10)


It turns out this only works if you use array indexes.

r = Route.find 10

Route Load (0.2ms) SELECT * FROM routes WHERE ( = 10)

a = r.appointments[0]

Appointment Load (1.2ms) SELECT * FROM appointments WHERE (appointments.route_id = 10)


Route Load (0.2ms) SELECT * FROM routes WHERE ( = 10)

h2. References

"inverseof not working with hasmany":


Just checking that you are human. What would be the result of this code?

a = 3*(4/2); b = 1; a+b

Ronnie said: Which team do you support? furosemide retention d'eau Dylan Staley, a junior at Louisiana State University, saidhe was unable to log into the state's federally operatedmarketplace around midnight on Tuesday because of a problemgetting past the security page, a frequently cited glitch.

Jaden said: I'd like to open a personal account ivermectina em mulheres grvidas The Army private first class was in Baghdad in 2010 when he was arrested and charged with leaking files, including videos of a 2007 attack by an American Apache helicopter gunship in Baghdad that killed a dozen people, including two Reuters news staff.

Danny said: I'm sorry, I'm not interested ordinare viagra per telefono The economy was a major factor in the decision to wait: The most common reasons for waiting were not having a car, being able to get around without driving, gas was too expensive and driving was too expensive. Few respondents cited the ability to connect online or GDL restrictions as a factor.

Shaun said: We were at school together the viagra hotel blackpool On July 30, which was a day off from Jets training camp, McKnight was pulled over in Hanover, N.J., for traffic violations when the police discovered two outstanding warrants for him for failure to pay traffic fines.

Grant said: Sorry, you must have the wrong number ibuprofen belupo 600mg As a result of the list being outdated, no 18-year-olds and only some 19-year-olds will be able to vote in the presidential election, which will go to a run-off on 11 August if there is no outright winner on Sunday.

Monroe said: I live here clindamycin mip a alkohol I see in previous comments that the right wing has got their knickers all tied up over this huge issue.  These meetings are nothing but photo ops anyway.  Who cares.  If there is any dialog it is going on with Kerry and his staff and Putin's staff on the other side.  And retribution for Snowden will happen without any of us even knowing it.  Maybe already happened.  Like who did send that virus to Iran that caused their centrifuges to self destruct?   

Alton said: I'm not sure argeflox ciprofloxacina 500 mg para que sirve The union is campaigning against plans to franchise or close 75 of the Crown offices - the larger branches usually sited in high streets - saying it will hit jobs, and adding that staff have not had a pay rise for more than two years.

Nigel said: Can I call you back? tadalafil accord 20 mg ra Ofcom admitted it is unlikely that operators will deploy 4G services in the newly-liberalised bands any time soon, but it insisted it is in the interests of consumers that these frequencies have been liberalised now.

Curtis said: I'm doing a masters in law tofranil for adhd reviews Tesco will provide a £80 million loan to the new business, and more than 4,000 employees will transfer to YFE Holdings Inc., an affiliate of Yucaipa, along with 150 of Fresh & Easy’s stores and its Riverside, Calif., distribution and production facilities.

Leandro said: We'd like to offer you the job how long does it take for lamisil to work Fortune's remains will lie in state in the Capitol rotunda in Hartford Thursday before taken by state police escort to Waterbury for a memorial service at the church where he was baptized and burial in a cemetery filled with prominent citizens. Plans call for bagpipers and the singing of "The Battle Hymn of the Republic."

Jack said: In conclusion, the timeline for a local or long-distance move can vary depending on several factors. By hiring a professional moving service, you can ensure that your move stays on track, giving you peace of mind during this stressful time. Auto Transport Rates Clinton County, Pennsylvania