Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2006, 9:34 PM   #1
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
My Rails Program With Relational DB

I'm sorry if this is going to seem like a contrite question but I am new to ruby on rails. I am creating a program for work to keep track of vehicles and their maintence and I am using a mysql relational database. I am having a hard time listing anything that besides the main parent table. That is I can list all the trucks we have that are in the truck table but I cannot list their last oilchanges and such. here are the db diagram and the acutal code (codes a bit messy)

http://sirbob1701.googlepages.com/VehicleLog.bmp
http://sirbob1701.googlepages.com/VehicleLog.zip
SirBob1701 is offline   Reply With Quote
Old May 30th, 2006, 4:51 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The reason for this is that Ruby on Rails can only find the relationship between two tables automatically if you adhere to certain conventions.

By default, Rails will look for a primary key called "id", and foreign key table connectors called "<tablename>_id". Your database does not adhere to these defaults, therefore Rails cannot automatically find the right connections. Here's some information on these "magic" field names.

To fix your problem, you need to either modify the database to adhere to the defaults, or explicitly tell Rails the names of the key columns (see here). For instance:

truck.rb:
class Truck < ActiveRecord::Base
	set_primary_key 'OilID'
	has_many :oils, :foreign_key => 'vehicleID'
end
oil.rb:
class Oil < ActiveRecord::Base
	set_primary_key 'vehicleID'
	belongs_to :truck, :foreign_key => 'vehicleID'
end
Arevos is offline   Reply With Quote
Old May 30th, 2006, 11:32 AM   #3
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
edit

I figured it out i should be
[code = oil.rb]
class Oil < ActiveRecord::Base
set_primary_key 'OilID'
belongs_to :truck, :foreign_key => 'vehicleID'
end
[/code]

[code = truck.rb]
class Truck < ActiveRecord::Base
set_primary_key 'vehicleID'
has_many :oils
end
[/code]
SirBob1701 is offline   Reply With Quote
Old May 30th, 2006, 11:39 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Oh, yes, sorry; my mistake. Got the primary keys mixed up
Arevos is offline   Reply With Quote
Old May 30th, 2006, 11:43 AM   #5
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
i'm trying this and it won't work. and i don't know if this is the right way to restrict
compile error
./script/../config/../app/views/oil/time.rhtml:49: parse error, unexpected '>'
./script/../config/../app/views/oil/time.rhtml:50: parse error, unexpected kDO, expecting kEND
	        <% truck.oil.each do |oil| ; _erbout.concat "\n"
	                            ^
./script/../config/../app/views/oil/time.rhtml:63: parse error, unexpected kEND, expecting $

Extracted source (around line #49): 

46: 	<td width="21%"><p align="center"><i><b>Is it Over Due? </b></i></td>
47:  <% @trucks.each do |truck| %>
48:  <tr>
49:    <% unless (truck.oil.vehicleID != truck.vehicleID?) %%>
50: 	        <% truck.oil.each do |oil| %>
51: 	<td><%= truck.vehicle %>
52: 	<td><%= oil.lastMile %></td>
SirBob1701 is offline   Reply With Quote
Old May 30th, 2006, 11:46 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Do you have an "end" for each "do"? If you start a block with "do", you need an "end" to tell Ruby where the block ends.
Arevos is offline   Reply With Quote
Old May 30th, 2006, 11:47 AM   #7
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
yes i do
here is all the code
 <% @trucks.each do |truck| %>
 <tr>
   <% unless (truck.oil.vehicleID != truck.vehicleID?) %%>
	        <% truck.oil.each do |oil| %>
	<td><%= truck.vehicle %>
	<td><%= oil.lastMile %></td>
	<td><%= oil.lastDate%></td>
	<td><%= oil.over%></td>
</tr>
<% end %>
</table>
<% end %>
</center>
SirBob1701 is offline   Reply With Quote
Old May 30th, 2006, 11:55 AM   #8
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
i probably don't need too loops too. but even when i take the top loop out i still get the error
SirBob1701 is offline   Reply With Quote
Old May 30th, 2006, 11:56 AM   #9
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
I changed it and now i get
compile error
./script/../config/../app/views/oil/time.rhtml:50: parse error, unexpected '>'
./script/../config/../app/views/oil/time.rhtml:52: unterminated string meets end of file

Extracted source (around line #50): 

47:  <% @trucks.each do |truck| %>
48:  <tr>
49:  <% truck.oil.each do |oil| %>
50:    <% unless (truck.oil.vehicleID != truck.vehicleID?) %%>
51: 	       
52: 	<td><%= truck.vehicle %>
53: 	<td><%= oil.lastMile %></td>
SirBob1701 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC