草庐IT

column-alias

全部标签

ruby-on-rails - alias_method、alias_method_chain 和 self.included

我在理解alias_method/alias_method_chain时遇到一点困难。我有以下代码:moduleActionView::HelpersmoduleFormHelperalias_method:form_for_without_cherries,:form_fordefform_for(record,options={},&proc)output='withacherryontop'.html_safeoutput.safe_concatform_for_without_cherries(record,options={},&proc)endendend这正是我想要的-将

ruby - 如何让 Ruby 中的 alias_method 使用子类的自定义方法?

假设我有一个包含三个子类的基类。基类有一个大多数子类通用的方法,它有一个别名:classBeerdefbottle_content'250ml'endalias_method:to_s,:bottle_contentendclassHeineken现在,如果在Duvel的分支子类实例上调用to_s方法,将返回250ml而不是330毫升。我明白为什么;别名是在父类(superclass)级别创建的。我知道这可以通过在发散类中重新定义alias_method来解决。但是还有其他方法吗?显然,使用to_s的方法是可行的:classBeerdefbottle_content'250ml'end

ruby-on-rails - ActiveRecord 加入 : integer columns converted to strings

我有一个名为Article的模型,我正在加入TwitterShare,如下所示:articles=Article.joins("LEFTOUTERJOINtwitter_sharesONarticles.id=twitter_shares.article_id").where("articles.id=?orarticles.id=?",27165,5632).select("articles.id,twitter_shares.user_id")当我取回文章并检查返回的Article模型时,它以字符串形式返回user_id,即使twitter_shares表中的列类型是整数。为什么是

ruby - 错误 : null value in column "id" violates not-null constraint

我刚刚将我的应用程序从mysql迁移到postgres但是当我尝试在特定表中插入记录时我得到violatesnot-nullconstraint错误:ERROR:nullvalueincolumn"id"violatesnot-nullconstraintDETAIL:Failingrowcontains(null,1,1,null,null,null,2016-03-0909:24:12.841891,2012-12-3123:00:00,2012-12-3123:00:00,null,null,f,null,f,XYZAssignment,null,null,null,null).

ruby-on-rails - ruby /rails : alias_method practices

我正在尝试覆盖Rails的“fields_for”方法,我目前正在这样做:moduleActionView::Helpers::FormHelperincludeActionView::Helpers::FormTagHelperalias_method:original_fields_for,:fields_fordeffields_for()#Someextrastuff#...output.safe_concatoriginal_fields_for()endend该功能运行良好,但我开始怀疑我对alias_method的使用不是最优雅的。最特别的是,如果我将此功能打包到一个ge

ruby-on-rails - 添加设备生成的用户表时,Rails 数据库迁移失败并显示 "duplicate column name: email"

我正在安装devise。我遵循了所有必需的步骤并在此处结束:$railsgeneratedeviseUser$rakedb:migrate当我运行rakedb:migrate时,出现以下错误:$rakedb:migrate==20140618020442AddDeviseToUsers:migrating=================================--change_table(:users)rakeaborted!StandardError:Anerrorhasoccurred,thisandalllatermigrationscanceled:SQLite3::

ruby-on-rails - 要扩展 Rails 的 `link_to` ,我应该使用 `alias_method_chain` 还是混合 + 继承?

我正在使用twitterbootstrap创建一个应用程序.我正在使用FontAwesome将图标添加到各个地方,通常是链接。到目前为止,我一直在使用全局助手。这是简化版本:#app/helpers/link_to_with_icon.rbdeflink_to_with_icon(text,path,options={})options=options.cloneicon=options.delete(:icon)text="#{text}"ificon.present?link_to(text,path,options)end这行得通,但每次我需要将图标添加到新链接时,我都必须记住将

ruby-on-rails - Rails - update_column 和 ActiveRecord 枚举

我需要在我的Rails应用程序中使用update_column来防止运行回调。我有带枚举的ActiveRecord模型:classCar在正常情况下(即当我确实希望回调运行时)我可以使用类似的东西:@car.update_attribute:state,:not_used是否可以用update_column做类似的事情?我正在使用Rails4.2 最佳答案 update_column不将符号识别为枚举的变体。我认为您必须使用Car.states[:not_used]而不是:not_used。像这样:@car.update_colum

ruby-on-rails - 分组错误 : ERROR: column "" must appear in the GROUP BY clause or be used in an aggregate function

我正在尝试创建一个已发表评论的唯一患者列表。代码字很好,直到我上传到heroku,它在postgresql中不起作用。这是我创建列表的Ruby.erb代码:Lastestcommentfromago@comments在Controller中定义为:defindex@comments=current_clinician.comments.order("created_atdesc")endheroku日志给我这个错误信息:PG::GroupingError:ERROR:column"comments.id"mustappearintheGROUPBYclauseorbeusedinana

ruby-on-rails - 使用 add_foreign_key : 'column "user_id"referenced in foreign key constraint does not exist' 进行 Rails 迁移

(Rails是5.0.0版本,Ruby是2.3.0p0)我想在我的用户表和卡片表之间创建关联。我已将belongs_to:user添加到Cards模型,并将has_many:cards添加到Users模型,并创建了一个迁移:classAddUserIdToCard当我运行rakedb:migrate时,我收到错误:ActiveRecord::StatementInvalid:PG::UndefinedColumn:ERROR:column"user_id"referencedinforeignkeyconstraintdoesnotexist:ALTERTABLE"cards"ADDC