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