Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   Manually add Primary key and Finds (http://www.programmingforums.org/showthread.php?t=10117)

SirBob1701 Jun 1st, 2006 8:37 AM

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.

Arevos Jun 1st, 2006 10:09 AM

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?

SirBob1701 Jun 1st, 2006 10:30 AM

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 Jun 1st, 2006 10:43 AM

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.

Arevos Jun 1st, 2006 11:14 AM

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


SirBob1701 Jun 1st, 2006 4:06 PM

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.

Arevos Jun 1st, 2006 4:53 PM

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 Jun 1st, 2006 5:00 PM

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?

SirBob1701 Jun 1st, 2006 6:05 PM

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.


All times are GMT -5. The time now is 1:23 AM.

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