User.rbRails不尊重模型關聯的HAS_ONE
has_one :subscription, :inverse_of => :user
Subscription.rb
has_one :credit_card, :inverse_of => :subscription
CreditCard.rb
belongs_to :user, :inverse_of => :credit_card
belongs_to :subscription, :inverse_of => :credit_card
在信用卡控制器:
def new
@credit_card = current_user.build_credit_card
end
def create
@credit_card = current_user.build_credit_card
end
if @credit_card.save
if @credit_card.save
format.html { redirect_to @credit_card, notice: 'Credit card was successfully created.' }
format.json { render action: 'show', status: :created, location: @credit_card }
else
format.html { render action: 'new' }
format.json { render json: @credit_card.errors, status: :unprocessable_entity }
end
end
不過,我仍然能夠多張信用卡添加到我的用戶模型。這怎麼可能?如果我是正確的,只有has_many應該發出這樣的行爲? has_one協會應該防止創建額外的實體,除了一個據我所知..
我試過所有的變化,但仍然沒有運氣。
任何幫助將不勝感激!
謝謝。
你是什麼意思,它的作品?什麼代碼可以添加多張卡片,以及什麼讓你認爲它不應該在工作? – dpassage
用戶擁有一張信用卡,但是任何給定的用戶仍然可以創建多張卡片?簡而言之,當它是has_one關聯時,它的行爲就好像是has_many。我希望這可以澄清我的問題。 – dsignr