1
在軌道4應用隨着has_many :through
關係不同的行與Rails的存儲陣列參數連接表
city.rb
has_many :business_type_cities
has_many :business_types, :through => :business_type_cities
business_type.rb
has_many :business_type_cities
has_many :cities, :through => :business_type_cities
businesstypecity .rb
belongs_to :buness_types
belongs_to :cities
business_types_controller.rb
def create
if business_type.save
BusinessTypeCity.create :business_type_id => business_type.id, :city_id => params[:cities]
flash[:notice] = 'Business Type Created Successfully!'
redirect_to admin_business_types_path
else
flash[:error] = business_type.errors.full_messages.join(', ')
end
end
但這裏city_id
是array
,我想存儲business_type_id
和city_id
一行,其他city_id
將不同行中存儲與同business_type_id
。
響應我得到的是以下幾點:
"business_type"=>{"name"=>"test", "review_type"=>"review_with_rating"}, "cities"=>["293", "1091", "1200"], "commit"=>"Create Business Type"
感謝
簡單,我需要迭代PARAMS [:城市]與每個做。雖然如果你有更好的答案,那麼對此感興趣。感謝您的時間。 –