previous_changes方法

本文展示了如何使用Rails中的Channel模型进行查找、验证、属性获取、更新等操作,并演示了如何正确地修改字符串类型的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


[27] pry(main)> c = Channel.find 6
=> #<Channel id: 6, title: "会员", cid: "96", pid: "", channel_position: "", data_type: "", filter_id: "", version: "1.2.0">
[28] pry(main)> c.valid?
=> true
[29] pry(main)>
[30] pry(main)>
[31] pry(main)> c.has_filter
=> "1"
[32] pry(main)> c.cid
=> "96"
[33] pry(main)> c.is_default_channel
=> "1"
[34] pry(main)> c.reload
=> #<Channel id: 6, title: "会员", cid: "96", pid: "", channel_position: "", data_type: "", filter_id: "", version: "1.2.0">
[35] pry(main)> c.previous_changes
=> {}
[36] pry(main)> c.title = c.title + 1
TypeError: no implicit conversion of Fixnum into String
from (pry):43:in `+'
[37] pry(main)> c.title = c.title + '1'
=> "会员1"
[38] pry(main)> c.save
=> true
[39] pry(main)> c.previous_changes
=> {"title"=>["会员", "会员1"], "updated_at"=>[Fri, 08 Apr 2016 08:10:30 CST +08:00, 2016-04-08 12:28:20 +0800]}
[40] pry(main)> a, b = [1,2]
=> [1, 2]
[41] pry(main)> a
=> 1
[42] pry(main)> b
=> 2


model里的private方法


  private
    def update_auto_activate_job
      if active?  # 对已经设置了自动开启时间的视频进行手动开启操作
        if (change = previous_changes[:state]) && change[0] == 0
          if job = Sidekiq::ScheduledSet.new.find_job(auto_activate_job_id)
            job.delete
          end

          update_attributes auto_activated_at: nil, auto_activate_job_id: nil 
        end
      else  # 针对未开启的视频,设置,取消或者更新定时开启时间
        if change = previous_changes[:auto_activated_at]
          original_value, new_value = change

          if original_value.nil?  # 设置定时开启, id为当前model的id
            job_id = AutoActivateWorker.perform_at new_value, id
            update_attribute :auto_activate_job_id, job_id
          elsif new_value.nil?  # 取消定时开启
            if job = Sidekiq::ScheduledSet.new.find_job(auto_activate_job_id)
              job.delete
            end

            update_attribute :auto_activate_job_id, nil 
          else # 更新定时开启时间
            if job = Sidekiq::ScheduledSet.new.find_job(auto_activate_job_id)
              job.reschedule new_value
            end
          end
        end
      end 
    end 




%% 步骤1:加载数据(新增当前档位) load(‘3.mat’, ‘CAN1_ETC2_TCM_TransSelectedGear_t0’, ‘t0’, ‘CAN1_EEC1_ENG_EngSpeed_t1’, ‘t1’,… ‘CAN1_EEC2_ENG_AccelPedalPos1_t2’, ‘t2’, ‘TCM_GrD_PctgRoadGrdt_t3’, ‘t3’,… ‘CAN1_ETC2_TCM_TransCurrentGear_t0’); % 添加当前档位数据 %% 步骤2:检测预选挡位变化点 pre_change_indices = find(diff(CAN1_ETC2_TCM_TransSelectedGear_t0) ~= 0) + 1; pre_change_times = t0(pre_change_indices); from_gear_pre = CAN1_ETC2_TCM_TransSelectedGear_t0(pre_change_indices-1); to_gear_pre = CAN1_ETC2_TCM_TransSelectedGear_t0(pre_change_indices); %% 步骤3:检测当前挡位变化点(油门≠0时) t2_round % 时间对齐预处理 t0_round = round(t0, 2); % 当前档位时间基准 t2_round = round(t2, 2); % 油门时间基准 % 获取当前挡位每个时刻的油门值 [~,idx] = min(abs(t2_round - t0_round’), [], 1); % 时间对齐匹配 current_acc = CAN1_EEC2_ENG_AccelPedalPos1_t2(idx); % 当前挡位对应油门值 % 检测变化点并过滤油门≠0 current_change = find(diff(CAN1_ETC2_TCM_TransCurrentGear_t0) ~= 0) + 1; valid_changes = current_acc(current_change) ~= 0; % 油门过滤条件 current_change = current_change(valid_changes); current_times = t0(current_change); from_current = CAN1_ETC2_TCM_TransCurrentGear_t0(current_change-1); to_current = CAN1_ETC2_TCM_TransCurrentGear_t0(current_change); %% 步骤4:匹配换挡事件 shift_time = nan(size(pre_change_times)); % 预初始化数组 for i = 1:length(pre_change_times) % 寻找相同档位变化的当前挡位事件 mask = (from_current == from_gear_pre(i)) &… (to_current == to_gear_pre(i)) &… (current_times >= pre_change_times(i)); if any(mask) [min_time, idx] = min(current_times(mask) - pre_change_times(i)); shift_time(i) = min_time; % 记录时间差 end end %% 步骤5:多信号匹配(沿用原有逻辑) t3_round search_times = round(pre_change_times, 2); t1_round = round(t1, 2); t3_round = round(t3, 2); [~,idx_rpm] = min(abs(t1_round - search_times’), [], 1); [~,idx_acc] = min(abs(t2_round - search_times’), [], 1); [~,idx_slope] = min(abs(t3_round - search_times’), [], 1); matched_rpm = CAN1_EEC1_ENG_EngSpeed_t1(idx_rpm); matched_acc = CAN1_EEC2_ENG_AccelPedalPos1_t2(idx_acc); matched_slope = TCM_GrD_PctgRoadGrdt_t3(idx_slope); %% 步骤6:生成综合结果表 result_table = table(… strcat(string(from_gear_pre),“→”,string(to_gear_pre)),… pre_change_times,… matched_rpm,… matched_acc,… matched_slope,… shift_time,… ‘VariableNames’,… {‘预选挡位变化’,‘预选挡位变化时间点(s)’,‘换挡前发动机转速’,‘油门开度(%)’,‘坡度(度)’,‘换挡时间(s)’}); writetable(result_table, ‘挡位变化综合记录.xlsx’); 在上述代码的基础上,增加发动机的实际扭矩(也是以t1为时间标准),修改上述代码,在完成上述功能的同时,增加找到预选挡位发生变化的前一个点的实际扭矩大小,并且找到当前档位发生变化此时间点的发动机转速(换挡后转速),生成的excel表格中再添加这两个量
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值