1
我有两个类:自定义保存在嵌套属性
class Post
has_and_belongs_to_many :receivers
accepts_nested_attributes_for :receivers
end
class Receiver
has_and_belongs_to_many :posts
表架构是这样的:
posts: id, xxx
receivers: id, email, name, xxxx
posts_receivers: post_id, receiver_id
我几乎如下莅临指导:http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
所以在我的post
形式,每个人可以包含几个接收者,这只是几个电子邮件文本字段。新的收件人记录会根据这些电子邮件自动创建。它会发生不同的post
表格有一些现有的电子邮件,那么我不想在现有的电子邮件receivers
表中创建新的记录。相反,我想找到与现有电子邮件的接收者ID并将ID保存到posts_receivers
表中。
现在它只是为新帖子创建新的收件人记录,无论该邮件是现有邮件还是新邮件。
有关如何实现这一点的任何建议?非常感谢!