Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 1st, 2006, 9:37 AM   #1
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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
      end
truck_vehicleID it does not insert the id properly. but if i remove set primary key from the truck.rb the method works? i know it most be some base somewhere but how can i fix it?

also 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
just prints out a blank screen event though there is a view for it.
SirBob1701 is offline   Reply With Quote
Old Jun 1st, 2006, 11:09 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
Quote:
Originally Posted by SirBob1701
truck_vehicleID it does not insert the id properly. but if i remove set primary key from the truck.rb the method works? i know it most be some base somewhere but how can i fix it?
What does the correspondin gview look like? Also, it might be useful if you could provide the code, a database schema and some test data. It's hard to understand the problem without context.

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:
Originally Posted by SirBob1701
just prints out a blank screen event though there is a view for it.
Again, what does the view look like?
Arevos is offline   Reply With Quote
Old Jun 1st, 2006, 11:30 AM   #3
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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.
SirBob1701 is offline   Reply With Quote
Old Jun 1st, 2006, 11:43 AM   #4
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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'")
    end

and 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.
SirBob1701 is offline   Reply With Quote
Old Jun 1st, 2006, 12:14 PM   #5
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 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
Arevos is offline   Reply With Quote
Old Jun 1st, 2006, 5:06 PM   #6
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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])
    end

I'm still running into errors though trying to write the primary key manually. Gonna have to look into that.
SirBob1701 is offline   Reply With Quote
Old Jun 1st, 2006, 5:53 PM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
You should be able to replace lines like this:
@oil = Oil.find(:all, :conditions => ['vehicleID = ?', id])
With lines like this:
@truck = Truck.find(@params["id"]) 
@oil = @truck.oils
The has_many and belongs_to constructs should make the two pieces of code equivalent in function.
Arevos is offline   Reply With Quote
Old Jun 1st, 2006, 6:00 PM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by SirBob1701
I'm still running into errors though trying to write the primary key manually. Gonna have to look into that.
What does your creation controller and view look like? Perhaps it's just a case of not getting the right ID name on creation?
Arevos is offline   Reply With Quote
Old Jun 1st, 2006, 7:05 PM   #9
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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
      end

the 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
end

and 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.
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 7:26 AM.

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