![]() |
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 |
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:
class Oil < ActiveRecord::Base |
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] |
Oh, yes, sorry; my mistake. Got the primary keys mixed up :P
|
i'm trying this and it won't work. and i don't know if this is the right way to restrict
:
compile error |
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.
|
yes i do
here is all the code :
<% @trucks.each do |truck| %> |
i probably don't need too loops too. but even when i take the top loop out i still get the error
|
I changed it and now i get
:
compile error |
| All times are GMT -5. The time now is 3:02 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC