1 Replies - 5214 Views - Last Post: 12 June 2012 - 06:03 AM Rate Topic: -----

#1 SanmanX  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 11-January 12

Mysql ruby conversion

Posted 11 June 2012 - 01:10 PM

Hey guys I need help converting a mysql query into ruby rails syntax. Thier are 3 tables that I'm joining: dept_location, permit_dept_location, and permit. The permit_dept_location table has a 2 columns named permit_id and dept_location_id. Here is the query I wrote in MySQL workbench that i want :

SELECT * FROM DeptTemp_development.permits p,
DeptTemp_development.permit_dept_locations pdl,
DeptTemp_development.dept_locations dl
WHERE pdl.permit_id= p.id
and pdl.dept_location_id = dl.id




Now here is what I wrote in ruby to try and get that same result:

	def totalCost
	@permits = Permit.find(:all)

	 @pdl =[]

	 @permits.each do |perm|
		@pdl <<	PermitDeptLocation.find_by_permit_id(perm.id) 
	 end
	 
	 @deptloc = []
	 @pdl.each do |dep|
 		@deptloc << DeptLocation.find_all_by_id(dep.dept_location_id)
	 end

	 @deptloc.fee_daily
	end



So this seems legit to me until i got an error for the dep.dept_location_id line, so i did i puts pdl.inspect to see what was going on. For some odd reason it's get 3 exact permits I want but also puts in 6 nils in the array. I have a told of 9 permits in my database, and 6 of those shouldn't get through the part :

@pdl <<	PermitDeptLocation.find_by_permit_id(perm.id) 


Any suggestions? Thx!

Is This A Good Question/Topic? 0
  • +

Replies To: Mysql ruby conversion

#2 SanmanX  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 11-January 12

Re: Mysql ruby conversion

Posted 12 June 2012 - 06:03 AM

OK i fixed it with a simple if statement, doing nothing if the item is nil. But I'm still confused why it's in their in the first place
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1