如何使用 has_many :through 和 in_place_edit ?
我有两个模型:营销活动和联系人。
一个营销活动有_许多联系人。
一个联系人有_许多个营销活动。
目前,每个联系人都有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将向您的 Campaigncontacts 资源添加一个名为 days_to_delay_communication_by 的整数字段,因为此信息与营销活动和联系人的关联相关,而不是与联系人本身相关。
在迁移中:
现在您可以通过以下方式设置该值:
然后在应用程序的管理端,您可以拥有一个控制器和一个营销活动通信视图,让您可以为营销活动联系人设置 days_to_delay_communication_by 字段。如果您有兴趣,我可以为您进一步扩展这一点,但我想您已经明白了。
然后,您需要运行某种后台进程(可能是 cron 作业,或使用 delayed_job 插件),查找尚未发生的通信,并在日期过后使它们发生。您可以在 rake 任务中执行此操作,如下所示:
然后您的 cron 作业将执行以下操作:
另外,我会考虑将连接模型的名称更改为更能描述其目的的名称,例如会员资格,一个活动有许多会员资格并且一个联系人有多个成员资格。然后,成员资格有一个 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:
Now you can set that value by:
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:
Then your cron job would do something like:
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.
做到这一点的一个好方法是在您的联系人模型上使用“假”属性,如下所示:
只需为这个假字段设置 in_place_editor :
然后
我不确定我是否完全理解您想要完成的任务,但我希望在至少指出你正确的方向。
A good way to do this is use a "fake" attribute on your Contact model like so:
Then just set the in_place_editor for this fake field:
and
I'm not sure I understood exactly what you wanted to accomplish, but I hope this at least points you into the right direction.