Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Solution below is assuming that status can be represented as a string.</p> <pre><code>class Campaign &lt; ActiveRecord::Base has_many :contacts end class Contact &lt; ActiveRecord::Base belongs_to :campaign has_many :contact_emails has_many :emails, :through =&gt; :contact_emails end class ContactEmail &lt; ActiveRecord::Base belongs_to :contact belongs_to :email belongs_to :user # add a column called status end class Email &lt; ActiveRecord::Base has_many :contact_emails belongs_to :contacts, :through =&gt; :contact_emails end </code></pre> <p>Adding emails to contact:</p> <pre><code>contact_email = @contact.contact_emails.build(:user =&gt; current_user, :email =&gt; @email, :status =&gt; "status1") contact_email.save </code></pre> <p>OR</p> <pre><code>@contact.contact_emails.create(:user =&gt; current_user, :email =&gt; @email, :status =&gt; "status1") </code></pre> <p>OR create multiple:</p> <pre><code>@contact.contact_emails.create( [ { :user =&gt; current_user, :email =&gt; @email, :status =&gt; "status1" }, { :user =&gt; current_user, :email =&gt; @email2, :status =&gt; "status2" } ] ) </code></pre> <p><strong>Edit 2</strong> Nested resources for ContactEmail.</p> <pre><code>map.resources :contacts, has_many :contact_emails </code></pre> <p>URL for ContactEmail</p> <pre><code>/contacts/1/contact_emails/new #new /contacts/1/contact_emails/2/edit #edit </code></pre> <p>The URL does not have the email id. You can pass the email_id as a query parameter, i.e.</p> <pre><code>new_contact_contact_email_path(@contact, :email_id =&gt; @email) </code></pre> <p>In your ContactEmailsController:</p> <pre><code>def new @contact = Contact.find(params[:contact_id]) @email = Email.find(params[:email_id]) @contact_email = @contact.contact_emails.build(:email =&gt; @email) end </code></pre> <p>In your view set <code>email_id</code> as hidden field. </p> <p>In the <code>create</code> method perform the save. </p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload