我有以下内容:
class Invite < ActiveRecord::Base
belongs_to :user
has_many :invite_recipients
has_many :recipients, :through => :invite_recipients
end
class InviteRecipient < ActiveRecord::Base
belongs_to :invite
belongs_to :user_comm
validates_associated :user_comm, :invite
validates_uniqueness_of :user_comm_id, :scope => :invite_id
end
class UserComm < ActiveRecord::Base
end
我想创建一个Invite方法,其中包含invite_text和UserComms列表作为变量,然后让它创建一个具有以下验证的新邀请:
1.所有UserComms都是唯一的
2.除非保存所有关联的InviteRecipients,否则不会保存邀请
(换句话说,除非所有创建的InviteRecipients都有效,否则邀请无效)
我不熟悉如何创建模型函数。而且,当我尝试这样的事情时:
i = Invite.new(:invite_text => 'come join')
ir1 = InviteRecipient.new(:invite => i, :user_comm => user_comm1)
ir2 = InviteRecipient.new(:invite => i, :user_comm => user_comm2)
i.invite_recipients = [uc1, uc2]
i.save!
我得到:SystemStackError:堆栈级别太深
答案 0 :(得分:0)
您需要使用i.recipients而不是invite_recipients! 像这样:
i.recipients.create(:user_comm => user_comm1)
i.recipients.create(:user_comm => user_comm2)