Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   My Rails Program With Relational DB (http://www.programmingforums.org/showthread.php?t=10059)

SirBob1701 May 29th, 2006 8:34 PM

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

Arevos May 30th, 2006 3:51 AM

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


SirBob1701 May 30th, 2006 10:32 AM

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]

Arevos May 30th, 2006 10:39 AM

Oh, yes, sorry; my mistake. Got the primary keys mixed up :P

SirBob1701 May 30th, 2006 10:43 AM

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>


Arevos May 30th, 2006 10:46 AM

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.

SirBob1701 May 30th, 2006 10:47 AM

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 May 30th, 2006 10:55 AM

i probably don't need too loops too. but even when i take the top loop out i still get the error

SirBob1701 May 30th, 2006 10:56 AM

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>



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