jjzjj

insert-update

全部标签

ruby - .insert 如何工作?

我想弄清楚insert函数在Ruby中的作用。我已经咨询过Google和ruby​​-doc.com,但解释不足以描述这个(看似)神秘的功能。这是让我感到困惑的地方:a=%w{abcd}putsa.insert(4,5)output=a,b,c,d,5第一个问题是,为什么没有插入4?putsa.insert(2,2,6)输出是:ab26cd由此提出的两个问题是为什么2没有插入两次?为什么2和6(看似)随意地放在b和c之间? 最佳答案 我不确定混淆是什么。来自Ruby文档:ary.insert(index,obj...)->aryIn

ruby-on-rails - 使用#update_all 更新时间戳

当我有要更新其属性的ID列表时,数据库中的updated_at字段似乎没有改变,这就是我的意思:ids=[2,4,51,124,33]MyObj.where(:id=>ids).update_all(:closed=>true)执行此更新后,updated_at字段不会更改。但是,当我使用railsc进入rails控制台并执行此操作时:obj=MyObj.find(2)obj.closed=false;obj.save!在此语句之后updated_at字段更改值。为什么是这样?当发生这种情况时,我正在监听更新并执行整个应用程序流程时,我依赖于我的应用程序中的这个updated_at字段

ruby-on-rails - 尝试更新数据库值时,rails update_attributes 返回 false

希望这里有人能给我指出正确的方向。我有一个ControllerUpdatedef运行“update_attributes”。目前它返回false,没有错误消息。我是Ruby的新手,但不是编码的新手,这让我困惑了好几天!我正在尝试使用下面指定的值更新用户模型和数据库。defupdate#getcurrentlyloggedinuser@user=current_user#updateuserparamsbasedoneditform...if@user.update_attributes(params[:user])redirect_toprofile_path,:notice=>"Su

关于Document mapping type name can‘t start with ‘_‘, found: [_update]

在修改elasticsearch时,用_update进行局部修改,修改失败,报错{    "error": {        "root_cause": [            {                "type": "invalid_type_name_exception",                "reason": "Document mapping type name can't start with '_', found: [_update]"            }        ],        "type": "invalid_type_name_exce

自动化update_post_meta

因此,我一直在研究此旧代码,并且正在尝试改进它。我有这个代码,从帖子中节省了许多自定义字段。if(isset($_REQUEST['unidade-dir1-cargo'])){update_post_meta($post_id,'unidade-dir1-cargo',sanitize_text_field($_POST['unidade-dir1-cargo']));update_post_meta($post_id,'unidade-dir1-nome',sanitize_text_field($_POST['unidade-dir1-nome']));update_post_meta(

ruby-on-rails - rails 中的 create_or_update 方法

ifClassName.exists?(["id=?",self.id])object=ClassName.find_by_name(self.name)object.update_attributes!(:street_address=>self.street_address,:city_name=>self.city_name,:name=>self.org_unit_name,:state_prov_id=>self.state_prov_id,:zip_code=>self.zip_code)elseClassName.create!:street_address=>self.

ruby-on-rails - 将 bool 值传递给 update_attributes 的问题

我有以下模型:classGuestCateringtruevalidates:order_number,:presence=>truevalidates:orderable,:presence=>trueend但是当我尝试使用以下代码更新现有的GuestCatering时:guest_catering.update_attributes(:orderable=>false)guestcatering变量是一个有效的GuestCatering对象。guest_catering对象更新后出现错误,像这样:nil}>但是当我传递一个orderable=>true时,一切都很好,没有错误。这里

ruby - bundler 能告诉我 Gemfile 中的哪些 gems 有更新的版本吗(例如 bundle update 的试运行)

有没有办法运行bundleupdate在假装模式下,类似于Rails生成器的-p(假装)标志或cap的-n(试运行)标志?我在想像这样的事情:$>bundleupdate-pFetchingsourceindexforhttp://rubygems.org/Thefollowinggemshaveupdatedversions:...listofgems... 最佳答案 Bundler1.1引入了一个新的“过时”功能,这正是我一直在寻找的。PatShaughnessy很棒write-up关于新功能。用他的话说,bundleoutda

ruby-on-rails - 如何在不触及 updated_at 属性的情况下更新单个属性?

我怎样才能做到这一点?试图创建2个方法,称为defdisable_timestampsActiveRecord::Base.record_timestamps=falseenddefenable_timestampsActiveRecord::Base.record_timestamps=trueend和更新方法本身:defincrement_pagehitupdate_attribute(:pagehit,pagehit+1)end使用如下回调打开和关闭时间戳:before_update:disable_timestamps,:only=>:increment_pagehitafte

ruby-on-rails - rails : Update model attribute without invoking callbacks

我有一个具有:credits属性的用户模型。我想要一个简单的按钮,它将通过名为“add”的路由将5添加到用户的积分中,以便/users/3/add将5添加到用户id=3的积分中。defadd@user=User.find(params[:id])@user.credits+=5redirect_toroot_pathend那是我Controller的相关部分。问题是,我不想调用@user.save,因为我有一个before_save回调,它根据当前的UTC时间重新加密用户的密码。我只想简单的给属性加5,避免回调,没想到这么简单的事情这么难。编辑:我将回调更改为:before_creat