在我的Ruby on Rails應用程序中,我創建了一些數據並將其保存在數據庫中,但現在我需要獲取記錄的id(保存時)。我需要得到這個ID,併爲此記錄,在其他表中創建綁定此id的記錄數組(在模型中我有關聯)。在rails中創建數據時獲取記錄的ID
PriceList.create(:distributor_id => distributor_id, :brand => brand, :article_nr => num_catalog, :price => price, :quantity => quantity, :waittime => waittime)
class PriceList < ActiveRecord::Base
has_many :cross_lists
belongs_to :distributor
end
class CrossList < ActiveRecord::Base
belongs_to :price_list
end
它可以看到作爲一個兩個問題,但主要的是第一部分。
爲什麼你要分配的主鍵?這是一個非常不安全的操作。或者你的意思是你想給你的新價格表'distributor_id'? – varatis
你的問題不清楚。顯示模型並請選擇更好的措辭和說明。 –
分配給一些變量說x和你可以得到id使用x.id –