How do I use has_many :through and in_place_edit?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-05-06T05:22:46Z Indexed on 2010/05/06 5:28 UTC
Read the original article Hit count: 131

Filed under:

I have two Models: Campaign and Contact.

A Campaign has_many Contacts.

A Contact has_many Campaigns.

Currently, each Contact has a contact.date_entered attribute. A Campaign uses that date as the ate to count down to the different Events that belong_to the Campaign.

However, there are situations where a Campaign for a specific Contact may need to be delayed by X number of days. In this instance, the campaigncontact.delaydays = 10.

In some cases, the Campaign must be stopped altogether for the specific Contact, so for now I set campaigncontact.delaydays = 1. (Are there major problems with that?)

By default, I am assuming that no campaigncontact exists (but not sure how that works?)

So here's what I've tried to do:

class Contact < ActiveRecord::Base
  has_many :campaigncontacts
  has_many :campaigns, :through => :campaigncontacts
end

class Campaign < ActiveRecord::Base
  has_many :campaigncontacts
  has_many :contacts, :through => :campaigncontacts
end

script/generate model campaigncontact campaign_id:integer contact_id:integer delaydays:integer

class Campaigncontact < ActiveRecord::Base
  belongs_to :campaign
  belongs_to :contact
end

So, here's the question: Is the above correct? If so, how do I allow a user to edit the delay of a campaign for a specific Contact.

For now, I want to do so from the Contact View.

This is what I tried:

In the Contact controller (?)

  in_place_edit_for :campaigncontact, column.delaydays

And in the View

<%= in_place_editor_field :campaigncontact, :delaydays %>

How can I get it right?

© Stack Overflow or respective owner

Related posts about has-many-through