Rails4:如何设置最大值 + 1 [英] Rails4: How to set max value + 1

查看:43
本文介绍了Rails4:如何设置最大值 + 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下模型:

课程表

我想做的是为添加的数据设置最大值+1.

例如更新前

第一天schedule_title A房间名称 A

更新后,

第一天schedule_title A房间名称 A第 2 天 # 我想在房间表中将 `2` 设置为 `day` 列schedule_title B房间名称 B

当我添加下一个房间时,我想将 2 设置为 day 列以获取房间表中的新数据.

schema.rb

 create_table "rooms", force: :cascade do |t|t.string "名称"t.integer "schedule_id"t.integer "day",默认值:1...结尾create_table "schedules", force: :cascade do |t|t.string "标题"t.integer "user_id"t.date "出发日期"...结尾

当我按下添加房间"按钮并在输入一些后更新时,我想将房间表中的新数据的最大值 + 1 设置为 day.

_schedule_form.html.erb

<%= simple_nested_form_for(@schedule) do |f|%><%= f.label :title %><%= f.text_field :title, class: 'form-control' %><%= f.label :departure_date %><%= f.text_field :departure_date, class: 'form-control' %><div id="房间"><%= f.simple_fields_for :rooms do |r|%><%= r.input :room %><%结束%><%= f.link_to_add "添加房间", :rooms, data: {target: '#room'}, class: "btn btn-primary" %>

<%结束%>

schedlues_controller.rb

class SchedulesController <应用控制器...定义更新如果@schedule.update(schedule_params)flash[:success] = "日程已更新!"重定向到 root_url别的渲染编辑"结尾结尾...私人的def schedule_paramsparams.require(:schedule).permit(:title, :departure_date, rooms_attributes: [:id, :_destroy, :room, :day])结尾

如果您能给我任何建议,我们将不胜感激.

解决方案

有两种方法.

方法 1:自己动手

尝试以下操作:

课程表

方法 2:使用现有实现

看看acts_as_list.position 相当于你的 date.

Give the following models:

class Schedule < ActiveRecord::Base
  has_many :rooms
  accepts_nested_attributes_for :rooms, allow_destroy: true
end

class Room < ActiveRecord::Base
  belongs_to :schedule
end

What I'd like to do is to set the maximum value + 1 to the added data.

For example, before update

Day 1
schedule_title A
    room_name A

after update,

Day 1
schedule_title A
    room_name A

Day 2    # I'd like to set `2` to `day` column in room table
schedule_title B
    room_name B

I'd like to set 2 to day column for new data in the rooms table when I add next room.

schema.rb

  create_table "rooms", force: :cascade do |t|
    t.string   "name"
    t.integer  "schedule_id"
    t.integer  "day",   default: 1
    ...
  end

  create_table "schedules", force: :cascade do |t|
    t.string   "title"
    t.integer  "user_id"
    t.date     "departure_date"
    ...
  end

When I press "Add room" button and update after entering some, I'd like to set maximum value + 1 to day for new data in room table.

_schedule_form.html.erb

<%= simple_nested_form_for(@schedule) do |f| %>

  <%= f.label :title %>
  <%= f.text_field :title, class: 'form-control' %>
  <%= f.label :departure_date %>
  <%= f.text_field :departure_date, class: 'form-control' %>

  <div id="room">
    <%= f.simple_fields_for :rooms do |r| %>
      <%= r.input :room %>
    <% end %>
    <%= f.link_to_add "Add room", :rooms, data: {target: '#room'}, class: "btn btn-primary" %>
  </div>

<% end %>

schedlues_controller.rb

class SchedulesController < ApplicationController
...
  def update

    if @schedule.update(schedule_params)
      flash[:success] = "schedule updated!"
      redirect_to root_url
    else
      render 'edit'
    end
  end 
...
  private

    def schedule_params
      params.require(:schedule).permit(:title, :departure_date,  rooms_attributes: [:id, :_destroy, :room, :day])
    end

It would be appreciated if you could give me any suggestion.

解决方案

There are two approaches.

Approach 1: Do it Yourself

Try the following:

class Schedule < ActiveRecord::Base
  has_many :rooms
  accepts_nested_attributes_for :rooms, allow_destroy: true
end

class Room < ActiveRecord::Base
  belongs_to :schedule
  before_create :set_date

  protected
  def set_date
    if self.date.nil?
      self.date = self.schedule.rooms.collect(&:date).max + 1
    end

    true
  end
end

Approach 2: Use an existing implementation

Take a look at acts_as_list. There position is equivalent to your date.

这篇关于Rails4:如何设置最大值 + 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆