我可以为 Rails 中的单个请求保存多次数据吗?

发布于 2024-11-27 19:14:28 字数 733 浏览 0 评论 0原文

有模型调用事件,其中包含以下属性

开始时间, 结束于, 细节, 培训师

这是我生成脚手架命令的正常创建方法

  def create
@event = Event.new(params[:event])

respond_to do |format|
  if @event.save
    format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
    format.xml  { render :xml => @event, :status => :created, :location => @event }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
  end
 end
end

,当特定事件对象 stat_at 和 end_at 之间的日期差距超过 6 时,我需要按如下方式修改此方法,我需要将该事件保存为两个事件。第一个活动将在原始开始日期开始,结束日期应为活动的中间日期,其他数据相同。但在第二个事件中,开始日期应该是中间日期,结束日期应该是原始结束日期。有人可以解释一下我该怎么做吗???

There is model call Event which contains following attributes

start_at,
end_at,
details,
trainer

This is my normal create method which generated my scaffold command

  def create
@event = Event.new(params[:event])

respond_to do |format|
  if @event.save
    format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
    format.xml  { render :xml => @event, :status => :created, :location => @event }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
  end
 end
end

and I need to modify this as follows when particular event object have date gap between stat_at and end_at more than 6 I need to save that event as two events. First event will started at original started date and end date should be middle date of the event and other data are same. But in second event it should be start date as middle date and end date should have original end date. Can some one explain how could I done this???

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

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

发布评论

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

评论(2

静谧幽蓝 2024-12-04 19:14:28

if @event.save 行之后,您可以像这样创建另一个事件记录

请注意,此代码未经测试。

if (@event.end_at - @event.start_at) > 6
  event2 = Event.new
  event2.start_at = something
  event2.end_at = anotherthing
  event2.save
end

After the line if @event.save you can create another Event record like this

Please note this code is untested.

if (@event.end_at - @event.start_at) > 6
  event2 = Event.new
  event2.start_at = something
  event2.end_at = anotherthing
  event2.save
end
半城柳色半声笛 2024-12-04 19:14:28

当您要执行一些逻辑时,您可以编写一个方法来执行逻辑并从控制器调用该方法。最好将方法保留在事件模型中,以满足胖模型和精益控制器的良好实践。

def self.create_event(ev_params)
  status = false
  if event.end_dat - event.stat_dat >6
    # create two events
    event1 = Event.new(ev_params)
    mid_date = event1.stat_at + ((event.end_dat - event.stat_dat)/2).days
    event1.end_at = mid_date
    event2 = Event.new(ev_params)
    event2.stat_at = mid_date
    status = event1.save'
    status = event=2.save'
  else
    status = Event.create(ev_params)
  end
  status      
end

并从控制器 @event.save 中的控制器 Event.create_event 调用此方法。

这不是经过测试的代码,但我希望您可以轻松地从上面的代码中获得。

As you are going to do some logic, you can write a method to perform your logic and call that method from controller. Better keep the method in Event model to satisfy good practice of Fat Model and Lean Controller.

def self.create_event(ev_params)
  status = false
  if event.end_dat - event.stat_dat >6
    # create two events
    event1 = Event.new(ev_params)
    mid_date = event1.stat_at + ((event.end_dat - event.stat_dat)/2).days
    event1.end_at = mid_date
    event2 = Event.new(ev_params)
    event2.stat_at = mid_date
    status = event1.save'
    status = event=2.save'
  else
    status = Event.create(ev_params)
  end
  status      
end

And call this method from controller Event.create_event in controller @event.save.

This is not tested code but I hope you can easily get from above code.

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