Я определил три модели (Пользователь основан на разработке).Несколько принадлежит_to и AssociationTypeMismatch
class Deal < ActiveRecord::Base
belongs_to :user
belongs_to :client #if i cut this line everything works fine
end
class Client < ActiveRecord::Base
has_many :deals
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :deals
end
В deals_controller.rb у меня есть:
def create
@deal = current_user.deals.build(deal_params) #ok
respond_to do |format|
if @deal.save
format.html { redirect_to @deal, notice: 'Deal was successfully created.' }
format.json { render action: 'show', status: :created, location: @deal }
else
format.html { render action: 'new' }
format.json { render json: @deal.errors, status: :unprocessable_entity }
end
end
end
При попытке создать новую сделку, я получаю ошибку:
ActiveRecord::AssociationTypeMismatch in DealsController#create
Client(#48283704) expected, got String(#16421928)
Extracted source (around line #31):
31 @deal = current_user.deals.build(deal_params) #ok
32 respond_to do |format|
Deal_params определяется как:
private
# Use callbacks to share common setup or constraints between actions.
def set_deal
@deal = Deal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def deal_params
params.require(:deal).permit(:client, :headline, :value, :description, :user_id)
end
end
Может кто-нибудь, пожалуйста, объясните мне ho w, чтобы пройти через это? Когда я удаляю «own_to: client» в модели сделки, все работает, но нет никаких отношений ...
Возможно, это потому, что столбец 'client' находится в таблице' deals'. Я прав? –
, пожалуйста, вставьте то, что вы получаете в параметрах при создании действия из журналов. –