![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
Manually add Primary key and Finds
I have the primary key for table truck set to be vehicleID and when i pass this method
def create
@a_truck = Truck.new(@params['truck'])
if @a_truck.save
redirect_to :action => 'list'
else
render_action 'new'
end
endalso I was wondering how i can display data from many finds. def found
@truck = Truck.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@oil = Oil.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@apperance = Apperance.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@inspection = Inspection.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@rdifferential = Rdifferential.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@tow = Tow.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@repair = Repair.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
end |
|
|
|
|
|
#2 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Also, consider posting on one of the Ruby on Rails forums/mailing lists. They probably know a lot more about the details of the system. Quote:
|
||
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
I fixed the first problem. Now the find does display i just can't figure out the variable name passed into the controller right now it defaults to 0.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
hers the input view
<form method="post" action="found"> <br> <b>Find by</b><br/> <b>Vehicle Number:</b><br/> <input id="truck_vehicleID" name="truck[vehicleID]" size="20" type="number" value=""/><br/> </p> <%= link_to "Find", :action => "found", :id => "@truck_vehicleID" %> </form> the controller def found
@truck = Truck.find(:all, :conditions => "trucks.vehicleID = 'truck_vehicleID'")
@oil = Oil.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@apperance = Apperance.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@brake = Brake.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@inspection = Inspection.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@rdifferential = Rdifferential.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@tow = Tow.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@tred = Tred.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
@repair = Repair.find(:all, :conditions => "vehicleID = 'truck_vehicleID'")
endand the output view (partial too big to put all here) <% @truck.each do |truck|%> <tr> <td><%= truck.vehicleID%></td> <td><%= truck.vin%></td> <td><%= truck.plate%></td> <td><%= truck.curmileage%></td> <td><%= truck.oilInt %></td> </tr><p> <% end %> The variable doesn't get passed properly so the found always comes up with vehicleID 0. I don't know if i have it named wrong in the controller or what. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
The controller seems odd. The code in it tries to find trucks with vehicleIDs equalling the string "truck_vehicleID".
Since vehicleID is the primary key, I suspect you could do something like: def found
@truck = Truck.find(@params['truck']['vehicleID'])
@oils = @truck.oils
...
end |
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
I actually broke it down like this and it works well.
def found
@truck = Truck.find(@params["id"])
find_others(@truck.vehicleID)
end
def find_others(id)
@oil = Oil.find(:all, :conditions => ['vehicleID = ?', id])
@apperance = Apperance.find(:all, :conditions => ['vehicleID = ?', id])
@brake = Brake.find(:all, :conditions => ['vehicleID = ?', id])
@inspection = Inspection.find(:all, :conditions => ['vehicleID = ?', id])
@rdifferential = Rdifferential.find(:all, :conditions => ['vehicleID = ?', id])
@tow = Tow.find(:all, :conditions => ['vehicleID = ?', id])
@tred = Tred.find(:all, :conditions => ['vehicleID = ?', id])
@repair = Repair.find(:all, :conditions => ['vehicleID = ?', id])
endI'm still running into errors though trying to write the primary key manually. Gonna have to look into that. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
You should be able to replace lines like this:
@oil = Oil.find(:all, :conditions => ['vehicleID = ?', id]) @truck = Truck.find(@params["id"]) @oil = @truck.oils |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
heres the create method
def create
@a_truck = Truck.new(@params['truck'])
if @a_truck.save
redirect_to :action => 'list'
else
render_action 'new'
end
endthe view <form method="post" action="create"> <br> <b>Vehicle Number:</b><br/> <input id="truck_vehicleID" name="truck[vehicleID]" size="20" type="number" value=""/><br/> <b>Vin:</b><br/> <input id="truck_vin" name="truck[vin]" size="20" type="number" value=""/><br/> <b>Current Mileage:</b><br/> <input id="truck_curmileage" name="truck[curmileage]" size="20" type="number" value=""/><br/> <b>Tag:</b><br/> <input id="truck_plate" name="truck[plate]" size="20" type="text" value=""/><br/> <b>Oil Change Interval:</b><br/> <input id="truck_oilInt" name="truck[oilInt]" size="20" type="number" value=""/><br/> </p> <input type="submit" value="Create"> </form> and just for measure the model class Truck < ActiveRecord::Base
set_primary_key :vehicleID
def before_create
self.id = vehicleID
end
def before_update
self.id = vehicleID
end
has_many :oils
has_many :tows
has_many :appearances
has_many :brakes
has_many :bridges
has_many :inspections
has_many :rdifferentials
has_many :repairs
has_many :treds
endand i might replace the @oil = Oil.find(:all, :conditions => ['vehicleID = ?', id]) but right now i wanna take care of some other stuff i can go back to it. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|