如何使用 has_many :through 和 in_place_edit ?

发布于 2024-08-31 15:57:30 字数 1147 浏览 7 评论 0原文

我有两个模型:营销活动和联系人。

一个营销活动有_许多联系人。

一个联系人有_许多个营销活动。

目前,每个联系人都有一个 contact.date_entered 属性。活动使用该日期作为属于该活动的不同事件的倒计时。

但是,在某些情况下,特定联系人的营销活动可能需要延迟 X 天。在本例中,campaigncontact.delaydays = 10。

在某些情况下,必须完全停止特定联系人的营销活动,因此现在我设置campaigncontact.delaydays = 1。(这有什么大问题吗?)

默认情况下,我我假设不存在营销活动联系人(但不确定它是如何工作的?)

所以这就是我尝试做的:

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

所以,问题是:以上正确吗?如果是这样,我如何允许用户编辑特定联系人的活动延迟。

现在,我想从联系人视图中执行此操作。

这是我尝试过的:

在联系人控制器(?)

  in_place_edit_for :campaigncontact, column.delaydays

和视图中

<%= in_place_editor_field :campaigncontact, :delaydays %>

我怎样才能做到正确?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-09-07 15:57:30

我将向您的 Campaigncontacts 资源添加一个名为 days_to_delay_communication_by 的整数字段,因为此信息与营销活动和联系人的关联相关,而不是与联系人本身相关。

在迁移中:

def self.up
  add_column(:campaigncontacts, :days_to_delay_communication_by, :integer)
end

def self.down
  remove_column(:campaigncontacts, :days_to_delay_communication_by)
end

现在您可以通过以下方式设置该值:

campaigncontact = Campaigncontacts.find(:first, :conditions => { :campaign_id => campaign_id, :contact_id => contact_id })
campaigncontact.days_to_delay_communication_by = 10

然后在应用程序的管理端,您可以拥有一个控制器和一个营销活动通信视图,让您可以为营销活动联系人设置 days_to_delay_communication_by 字段。如果您有兴趣,我可以为您进一步扩展这一点,但我想您已经明白了。

然后,您需要运行某种后台进程(可能是 cron 作业,或使用 delayed_job 插件),查找尚未发生的通信,并在日期过后使它们发生。您可以在 rake 任务中执行此操作,如下所示:

namespace :communications do
  namespace :monitor do
    desc 'Monitor and send communications for campaigns'
    task :erma => :environment do
      Rails.logger.info "-----BEGIN COMMUNICATION MONITORING-----"

      unsent_communications = Communication.all(:conditions => { :date_sent => nil})
      unsent_communications.each do |communication|
        Rails.logger.info "**sending communication**"
        communication.send if communication.time_to_send < Time.now
        Rails.logger.info "**communication sent**"
      end

      Rails.logger.info "-----END COMMUNICATION MONITORING-----"
    end #end erma task
  end #end sync namespace
end #end db namespace

然后您的 cron 作业将执行以下操作:

cd /path/to/application && rake communications:monitor RAILS_ENV=production

另外,我会考虑将连接模型的名称更改为更能描述其目的的名称,例如会员资格,一个活动有许多会员资格并且一个联系人有多个成员资格。然后,成员资格有一个 days_to_delay_communication 字段。

I would add an integer field to your Campaigncontacts resource called days_to_delay_communication_by, since this information relates to the association of a campaign and a contact rather than a contact itself.

in your migration:

def self.up
  add_column(:campaigncontacts, :days_to_delay_communication_by, :integer)
end

def self.down
  remove_column(:campaigncontacts, :days_to_delay_communication_by)
end

Now you can set that value by:

campaigncontact = Campaigncontacts.find(:first, :conditions => { :campaign_id => campaign_id, :contact_id => contact_id })
campaigncontact.days_to_delay_communication_by = 10

Then in the admin side of your application you can have a controller and a view for campaign communications that lets you set the days_to_delay_communication_by field for campaigncontacts. I can expand on this further for you if you're interested, but I think you get the idea.

Then you'll need to run a background process of some sort (probably a cron job, or use the delayed_job plugin), to find communications that haven't happened yet, and make them happen when the date has passed. You could do this in a rake task like so:

namespace :communications do
  namespace :monitor do
    desc 'Monitor and send communications for campaigns'
    task :erma => :environment do
      Rails.logger.info "-----BEGIN COMMUNICATION MONITORING-----"

      unsent_communications = Communication.all(:conditions => { :date_sent => nil})
      unsent_communications.each do |communication|
        Rails.logger.info "**sending communication**"
        communication.send if communication.time_to_send < Time.now
        Rails.logger.info "**communication sent**"
      end

      Rails.logger.info "-----END COMMUNICATION MONITORING-----"
    end #end erma task
  end #end sync namespace
end #end db namespace

Then your cron job would do something like:

cd /path/to/application && rake communications:monitor RAILS_ENV=production

Also, I'd consider changing the name of your join model to something more descriptive of it's purpose, for instance memberships, a campaign has many memberships and a contact has many memberships. Then a membership has a days_to_delay_communication field.

子栖 2024-09-07 15:57:30

做到这一点的一个好方法是在您的联系人模型上使用“假”属性,如下所示:

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

  attr_accessor :delay

  def delay #edit
    self.campaigncontacts.last.delaydays
  end

  def delay=(val)
    self.campaigncontacts.each do |c|
      c.delaydays = val
    end
  end
end

只需为这个假字段设置 in_place_editor :

in_place_edit_for :contact, :delay

然后

<%= in_place_editor_field :contact, :delay %>

我不确定我是否完全理解您想要完成的任务,但我希望在至少指出你正确的方向。

A good way to do this is use a "fake" attribute on your Contact model like so:

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

  attr_accessor :delay

  def delay #edit
    self.campaigncontacts.last.delaydays
  end

  def delay=(val)
    self.campaigncontacts.each do |c|
      c.delaydays = val
    end
  end
end

Then just set the in_place_editor for this fake field:

in_place_edit_for :contact, :delay

and

<%= in_place_editor_field :contact, :delay %>

I'm not sure I understood exactly what you wanted to accomplish, but I hope this at least points you into the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文