草庐IT

MySQL 更新触发器 - 查找更改的列?

全部标签

ruby-on-rails - 一次交易下创建和更新多个模型

我想知道在Rails中是否可以在一个事务下进行多次更新和创建。我想创建一个no。来自任何数组的Products。但是对于每个产品,我还需要为其创建Company和Category。所以思路是这样的--Startatransaction//createacompany//createacategorywhileproduct_list{//createaproductwithcompanyandcategorycreatedabove}--endatranscation因此,如果任何创建失败,我希望回滚较早的更新/创建。 最佳答案 b

ruby-on-rails - Rails 如何在渲染 json 时更改属性名称?

在我的Controller中我有:@pakkes=Pakke.where("navnlike?","%#{params[:q]}%")respond_todo|format|format.html#index.html.erbformat.xml{render:xml=>@pakkes}format.json{render:json=>@pakkes.map(&:attributes)}end如何在渲染JSON时将属性navn更改为name? 最佳答案 您可以使用Pakke中的一行方法完成此操作:defas_json(*args)s

ruby-on-rails - Rails_Admin - 如何更改 Post View 中文本字段的大小

RailsAdmin中Post的“正文”输入区域的默认高度非常小。我想弄清楚如何增加高度。有什么建议么?config.modelPostdolabel'Blog'weight0editdofield:userfield:titlefield:body_formatfield:bodydo(somethinghere?)end 最佳答案 configure:descriptiondohtml_attributesrows:20,cols:50end 关于ruby-on-rails-Rail

ruby - 在 ruby​​ 中查找任意正则表达式的最后(最右边)匹配

我正在用ruby​​开发一个文本编辑器,我需要使用用户提供的正则表达式模式来支持“查找”功能。这是一个简单(熟悉的)用例:JoeUseriseditingatextfile,andhaspositionedthecursorsomewhereinthemiddleofthefile.Hewantstosearchbackwardsfromthecurrentcursorlocationforthenearestsubstringmatchinganarbitraryregularexpression.我认为这个问题相当于将用户的模式应用于文件中光标位置之前的整个字符串。当然,我可以从文

ruby - 如何设置mysql2时区选项以删除查询警告

使用mysql2做查询总是得到警告/usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:463:warning::database_timezoneoptionmustbe:utcor:local-defaultingto:local我确实看到了时区选项Mysql2现在支持两个时区选项::database_timezone-thisisthetimezoneMysql2willassumefieldsarealreadystored

ruby - 不断查找

场景#1:在下面的示例中,putsPost::User.foo行打印foo。换句话说,Post::User返回一个全局的User常量。classUserdefself.foo"foo"endendclassPostputsPost::User.fooend#=>warning:toplevelconstantUserreferencedbyPost::User#=>foo场景#2:第二个示例会引发错误,因为未找到常量。moduleUserdefself.foo"foo"endendmodulePostputsPost::User.fooend#=>uninitializedconsta

ruby-on-rails - 将 Rails/ClearDB App 推送到 Heroku 错误 'Can' t 连接到 '127.0.0.1' 上的 MySQL 服务器

每次我跑:gitpushherokumaster我收到以下错误:Running:rakeassets:precompilerakeaborted!Can'tconnecttoMySQLserveron'127.0.0.1'我在运行rails-vRails3.2.11和ruby-vruby1.9.3p194(2012-04-20revision35410)[x86_64-darwin12.2.0]我已经通过HerokuCLI安装了ClearDB,它似乎工作正常,但我无法找出这个错误。这是我用于生产的yml:production:adapter:mysql2encoding:utf8hos

ruby - 从数组中查找符合条件的前 n 个元素

我想选择一个数组中符合特定条件的前10个元素,而不必遍历整个数组。我知道find给我第一个元素。例如,下面的代码给出了第一个大于100的素数:require'prime'putsPrime.find{|p|p>100}#=>101有没有办法得到前10个大于100的素数? 最佳答案 在Ruby2.0+中你可以这样写:require'prime'Prime.lazy.select{|p|p>100}.take(10).to_a#=>[101,103,107,109,113,127,131,137,139,149]

ruby-on-rails - 如何在不使用不同变量名的情况下更新 Rails 模型中实例方法中的属性?

我想在rails的实例方法中更新属性,而不必被迫更改传入的参数,这样我就可以利用rails的自动属性。这是一个例子。理想:status="some_new_status"person.update(status)classPerson我现在要做的:classPerson我明白在这个例子中这并不重要。但是,当我有复杂的更新方法时,如果我可以删除其中的一些代码,它会更清晰。 最佳答案 你应该使用内置的Rails方法:@person.update_attribute(:status,"SomeValue")#nocallbacktrigg

ruby - 枚举器 `Array#each` 's {block} can' t 总是更改数组值?

好吧,也许这很简单,但是......鉴于此:arr=("a".."z").to_aarr=>["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]..我正在尝试将所有“arr”值更改为“bad”为什么这行不通?arr.each{|v|v="bad"}arr=>["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v"