I have two classes:
class Post
has_and_belongs_to_many :receivers
accepts_nested_attributes_for :receivers
end
class Receiver
has_and_belongs_to_many :posts
table schema is like:
posts: id, xxx
receivers: id, email, name, xxxx
posts_receivers: post_id, receiver_id
I pretty much follows the guide here: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
So in my post form, each one could contain several receivers, which are just several email text fields. New receiver records get created automatically from those emails. It would happen that different post form has some existing emails, then I dont want to create new records in receivers table for existing emails. Rather, I would like to find the receiver id with existing email and save the id with post id into posts_receivers table.
Now it just creates new receiver record every time for new posts, no matter the email is an existing one or a new one.
Any suggestion on how to implement this? Thanks a lot!