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 [:城市]与每个做。虽然如果你有更好的答案,那么对此感兴趣。感谢您的时间。 –