简体   繁体   English

如何通过在rails中通过关联对has_many进行关联来实现has_many?

[英]How do I do a has_many through association on a has_many through association in rails?

Warning: I may have the wrong 'problem statement' but here it goes: 警告:我可能输入了错误的“问题说明”,但问题出在这里:

A Campaign has many Contacts. 一个广告系列有很多联系人。

A Campaign has many Emails. 一个广告系列有很多电子邮件。

Therefore, a Contact has many Emails through a Campaign. 因此,联系人通过活动有许多电子邮件。

And an Email can have many Contacts through a Campaign. 电子邮件可以通过一个广告系列拥有许多联系人。

Each Contact-Email pair has its own unique Status (status1, status2, etc). 每个“联系人-电子邮件”对都有自己独特的状态(状态1,状态2等)。

Each Status (for a Contact-Email pair) will have its own User. 每个状态(用于“联系人-电子邮件”对)将拥有自己的用户。

I do not know how to model Status, or User. 我不知道如何为状态或用户建模。 Currently the immediate challenge is Status. 当前最直接的挑战是地位。

(see diagram below) (参见下图)

替代文字

Solution below is assuming that status can be represented as a string. 以下解决方案假定状态可以表示为字符串。

class Campaign < ActiveRecord::Base
  has_many :contacts
end

class Contact < ActiveRecord::Base
  belongs_to :campaign
  has_many :contact_emails
  has_many :emails, :through => :contact_emails   
end

class ContactEmail < ActiveRecord::Base
  belongs_to :contact
  belongs_to :email
  belongs_to :user
  # add a column called status 
end

class Email < ActiveRecord::Base
  has_many :contact_emails
  belongs_to :contacts, :through => :contact_emails
end

Adding emails to contact: 添加电子邮件以联系:

contact_email = @contact.contact_emails.build(:user => current_user, 
      :email => @email, :status => "status1")

contact_email.save

OR 要么

@contact.contact_emails.create(:user => current_user, 
  :email => @email, :status => "status1")

OR create multiple: 或创建多个:

@contact.contact_emails.create(
  [
    {
      :user => current_user, 
      :email => @email, 
      :status => "status1"
    },
    {
      :user => current_user, 
      :email => @email2, 
      :status => "status2"
    }
  ]
)

Edit 2 Nested resources for ContactEmail. 为ContactEmail 编辑2个嵌套资源。

map.resources :contacts, has_many :contact_emails

URL for ContactEmail ContactEmail的URL

/contacts/1/contact_emails/new #new
/contacts/1/contact_emails/2/edit #edit

The URL does not have the email id. 该网址没有电子邮件ID。 You can pass the email_id as a query parameter, ie 您可以将email_id作为查询参数传递,即

new_contact_contact_email_path(@contact, :email_id => @email)

In your ContactEmailsController: 在您的ContactEmailsController中:

def new
  @contact = Contact.find(params[:contact_id])
  @email   = Email.find(params[:email_id])
  @contact_email = @contact.contact_emails.build(:email => @email)
end

In your view set email_id as hidden field. 在您的视图中,将email_id设置为隐藏字段。

In the create method perform the save. create方法中执行保存。

def create
  @contact = Contact.find(params[:contact_id])
  @contact_email = @contact.contact_emails.build(params[:contact_email])
  if @contact_email.save
    # success
  else
    # error
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM