Reputation: 9184
I have what I think is a non standard model strcture (i know that it is bad, but its neccessary). Given the following migration :
create_table :MODELS, :primary_key => :MOD_ID do |t| t.integer :MOD_ID t.integer :MOD_MFA_ID t.integer :MOD_CDS_ID t.integer :MOD_PCON_START t.integer :MOD_PCON_END t.integer :MOD_PC t.integer :MOD_CV t.integer :MOD_AXL t.binary :MOD_PC_CTM t.binary :MOD_CV_CTM t.timestamps
The MOD_MFA_ID
corresponds to an association to another table/model (manufacturers
). I would like to retrieve models based on the manufacturer's brand column.
def getmanufacturer
@manufacturer = Manufacturers.find(params[:manufacturer])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @manufacturer }
end
end
I can't figure out how to modify my controller and routing to handle this use case. I'd like to query the app using this kind or URL : /models?manufacturer=audi&&model=a6
Upvotes: 0
Views: 183
Reputation: 10099
If you have a migration and are creating a new database, then you should make a standard db. I understand if you are connecting to a database that you were not able to design. I'd also use lowercase for the field names.
If you want MFA_BRAND to be the primary key, then you can set the primary key in your class. Suppose you want MFA_ID to be the primary key, then you would add this to your class as:
class Model < ActiveRecord::Base
set_primary_key "MFA_ID"
end
This will change the find and to_param methods to use your primary key field.
However, assuming you don't want to change the primary key, but sometimes want to search by MFA_BRAND, then you will want to pass the MFA_BRAND value as a CGI parameter. First set teh routes as
resources :models do
collection do
get :getmanufacturer
end
end
When you need the path helper call it this way:
getmanufacturer_models_path(:manufacturer => "Acme")
the URL will be
/models/getmanufacturer?manufacturer=Acme
Upvotes: 0
Reputation: 1484
So saying you want a URL like:
/manufacture/audi/model/a6
in your routes file do:
resources manufactures do
resources models
end
For the models you would want to switch your primary key
Manufacturer model:
set_primary_key :manufacturer_name #what ever you want used instead of ID
Do the same thing for your MODEL model
set_primary_key :MOD_Name
Then in your controller you can do:
Manufacturer.find(params[:manufaturer_id]).models.find(params[:id])
If you don't want to switch the primary keys, you can also do find_by:
Manufacturer.find_by_manufacturer_name("Audi").models.find_by_model_name("a6")
Upvotes: 3