it’s possible make a dropdown menu where menu’s fields are datas that comes from several mysql tables? I explain better: suppose that I’ve two models “Hardwares” and “Brands” and suppose that:
class Hardware < ActiveRecord::Base
belongs_to :brand
end
and
class Brand < ActiveRecord::Base
has_many :hardwares
end
and suppose that Hardware migration is:
class Hardwares < ActiveRecord::Migration
def self.up
create_table "hardwares" do |t|
t.column "model", :string
t.column "id", :integer
t.column "brand_id", :integer
end
end
def self.down
drop_table "hardwares"
end
I’d want that each dropdown menu element is something like that:
@hw=Hardwares.find(params[:id])
[@hw.brand.name,@hw.model] <- Dropdown menu's element
In witch way can I do this?
Thank you all in advance
As I understand it, you want to associate Hardware to a Project but when listing the Hardware you want to include the brand name to avoid a list of model numbers.
Update: Define the following in your Hardware model:
Unfortunately, the Rails API docs don’t allow linking directly to methods but what you want is collection_select.
In a form, you can create a select element menu using the following method:
This will provide a dropdown menu containing all the brand names allowing you to pick the correct one for the hardware.