![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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 class Oil < ActiveRecord::Base set_primary_key 'vehicleID' belongs_to :truck, :foreign_key => 'vehicleID' end |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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] |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Oh, yes, sorry; my mistake. Got the primary keys mixed up
![]() |
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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> |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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> |
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
i probably don't need too loops too. but even when i take the top loop out i still get the error
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|