草庐IT

ruby-on-rails - rails 2.3 : How to turn this SQL statement into a named_scope

弄清楚如何从这个SQL查询创建一个named_scope有点困难:select*fromfoowhereidNOTIN(selectfoo_idfrombar)ANDfoo.category=?按RAND()限制1排序;类别应该是可变的。针对上述问题编写命名范围的最有效方式是什么? 最佳答案 named_scope:scope_name,lambda{|category|{:conditions=>["idNOTIN(selectfoo_idfrombar)ANDfoo.category=?",category],:order=>'

ruby-on-rails - 如何使 named_scope 与连接表一起正常工作?

这是我的情况。我有两个表:质押和质押交易。当用户做出promise时,他在promise表中只有一行。稍后当需要履行promise时,每笔付款都会记录在我的pledge_transactions表中。我需要能够查询到所有未结质押,即交易表中的金额之和小于质押金额。这是我目前所拥有的:named_scope:open,:group=>'pledges.id',:include=>:transactions,:select=>'pledge_transactions.*',:conditions=>'pledge_transactions.idisnotnullorpledge_trans

ruby - Rails,防止 Model.scoped 的弃用警告,找到(:all) and relation #all?

我有通过但显示的测试$rspecspec/event_calendar_spec.rb......DEPRECATIONWARNING:Model.scopedisdeprecated.PleaseuseModel.allinstead.(calledfromevents_for_date_rangeat/home/durrantm/Dropbox/96_2013/work/code/ruby/event_calendar/lib/event_calendar.rb:52)DEPRECATIONWARNING:Calling#find(:all)isdeprecated.Pleasec

ruby-on-rails - 如何对所有 ActiveRecord 模型使用通用的 named_scope

你好,如何构建对所有模型通用的命名范围。 最佳答案 我通过将这段代码放在lib/has_common_named_scopes.rb中来做到这一点:moduleHasCommonNamedScopesdefself.included(base)base.class_eval{#Namedscopesnamed_scope:newest,:order=>"#{base.table_name}.created_atDESC"named_scope:freshest,:order=>"#{base.table_name}.updated_

ruby-on-rails - rails : How to add scope to pg_search with location search?

我有两个型号,Location和Basic,我使用的是pg_search和geocodergem,如何使用geocodernear方法将范围添加到我的pg_search_scope或者是否有其他方法可以这样做?Location模型有纬度和经度列。在我的Controller中我可以这样做:#thisallowsmetosearchneartheLocationmodel@business=Location.near(params[:loc],15)#thissearchesthroughthepg_search_scope@term=Basic.search(params[:q])#I'

c++ - GCC 错误 : explicit specialization in non-namespace scope

我正在尝试移植以下代码。我知道标准不允许在非名称范围范围内进行显式特化,我应该使用重载,但我只是找不到在这种特殊情况下应用这种技术的方法。classVarData{public:templateboolIsTypeOf(intindex)const{returnIsTypeOf_f::IsTypeOf(this,index);//noerror...}templateboolIsTypeOf(intindex)const//error:explicitspecializationinnon-namespacescope'classStateData'{returnfalse;}temp

c++ - GCC 错误 : explicit specialization in non-namespace scope

我正在尝试移植以下代码。我知道标准不允许在非名称范围范围内进行显式特化,我应该使用重载,但我只是找不到在这种特殊情况下应用这种技术的方法。classVarData{public:templateboolIsTypeOf(intindex)const{returnIsTypeOf_f::IsTypeOf(this,index);//noerror...}templateboolIsTypeOf(intindex)const//error:explicitspecializationinnon-namespacescope'classStateData'{returnfalse;}temp

ruby-on-rails - 如何取消 join/eager_load 中的 default_scope?

我有两个模型:classUserdefault_scope->{where(deleted_at:nil)}endclassOrderbelongs_to:userend我想获得已删除或未删除用户的订单:Order.joins(:user).merge(User.unscoped)Order.joins(:user).merge(User.unscope(where::deleted_at))#SELECT"orders".*FROM"orders"#INNERJOIN"users"ON"users"."id"="orders"."user_id"AND"users"."deleted

ruby-on-rails - Rails3 如何使用 :params in named scope?

我正在尝试显示特定订单的里程碑列表。(订单有很多里程碑。)在我的订单模型中,我有这个:scope:open,lambda{joins("joinmilestonesonmilestones.order_id=orders.id").where("order_id=?ANDmilestone_status=?",:params[:order_id],true).group("orders.id")}我遇到的问题是让当前订单ID起作用-:params[:order_id]显然是错误的。在我的route我有这个:resources:ordersdoresources:milestonesen

ruby-on-rails - rails 和 ActiveRecord : DRY use same logic in scope and boolean method

我有一个带有范围和方法的模型,如下所示:classModel?OR(updated_atISNULLANDcreated_at>?)',(Date.today-3.days).beginning_of_day,(Date.today-3.days).beginning_of_day)defeditable?return(self.updated_at||self.created_at)>(Date.today-3.days).beginning_of_dayendend我觉得我不应该在范围和方法中编写两次相同的逻辑。有什么办法可以避免这种情况吗?我在Rails3.2上谢谢