나는 특정 테이블에 기본 값을 추가하는 방법을 볼 수 있습니다쿼리이 SO 기사에서
Add a default value to a column through a migration
change_column :shops, :currency_id, :integer, :default => 1
내가 가지고 다른 테이블 통화가 ID 및 ISO_Name. 시스템에서 EUR를 기본값으로 사용하길 원합니다. 그러나 이것이 ID 5 또는 ID 1을 가질 수도 있습니다. 또는
그럼 내 질문 : 쿼리 결과를 기반으로하는 기본값을 어떻게 정의 할 수 있습니까? . 당신이 통화 ISO_NAME 필드가로
class SetDefaultCurrencyForShops < ActiveRecord::Migration
def up
currency = Currency.find_by_iso_code('EUR')
if currency
change_column :shops, :currency_id, :integer, :default => currency.id
end
end
end
필드가 iso_name 인 경우, iso_code를 사용하는 이유는 무엇입니까? change_column : stores, : currency_id, : integer, : default => Currency.find_by_iso_name ('EUR'). id –
당신은 절대적으로 옳습니다. Currency.find_by_iso_name ('EUR') .id가 작동합니다. 이 부분을 추가하면 답변으로 표시됩니다. – rept
잘 작동했는지 확인하는 것이 좋습니다. 그건 그렇고 대답으로 그것을 추가했습니다 :) –