草庐IT

aRelation

全部标签

ruby-on-rails - Arel `between` 以其他列为范围

似乎Arel的Between谓词只能与范围一起使用,例如between(1.day.ago.time..Time.current)有谁知道将它与其他列一起使用的方法吗?喜欢between(table[:since]..table[:till])(最后一个不起作用,但它显示了一个想法)。最后我要columnBETWEENtable.sinceANDtable.till 最佳答案 要生成table.fieldBETWEENanother_table.fromANDanother_table.to使用以下代码:Arel::Nodes::B

sql - Arel 中的 SQL WITH RECURSIVE 等价物

我有以下SQL,它使用WITHRECURSIVE递归获取记录的所有父项。在阿雷尔,这相当于什么?table_name=self.class.table_namearel_table=self.class.arel_tablesql= 最佳答案 在查看了@cschroed向我指出的测试后,我能够将我的原始SQL重构为:deflineagehierarchy=Arel::Table.new:hierarchyrecursive_table=Arel::Table.new(table_name).alias:recursiveselect

ruby-on-rails - 有什么方法可以使用 AREL 进行自定义关联吗?

modelPost#ActiveRecordassociationshavetonsofoptionsthatlet#youdojustaboutanythinglike:has_many:commentshas_many:spam_comments,:conditions=>['spammy=?',true]#InRails3,namedscopesareultra-elegant,andletyoudothingslike:scope:with_comments,joins(:comments)end有没有什么方法可以使用AREL或其他更精简的语法来像命名范围一样优雅地定义自定义关

ruby - 我可以使用 AREL/ActiveRecord 更优雅地编写此查询吗?

我能否使用AREL/ActiveRecordAPI将此查询写得更短和/或更优雅?Foo.where("(barisnotnullandbar!='')or(bazisnotnullandbaz!='')") 最佳答案 您可以直接对Arel执行OR运算符,但语法不是很漂亮并且可能有点难以阅读。这是它在arel中的样子:foo=Foo.arel_tableFoo.where(foo[:bar].not_eq(nil).and(foo[:bar].not_eq('')).or(foo[:baz].not_eq(nil).and(foo[:

ruby - Rails3 Arel 访问自定义类

我有一个自定义类,但我希望能够将它传递给Arel并让它解析它的可查询部分。moduleCustomclassItemattr_accessor:namedefinitialize(name)self.name=nameendendendcustom_item=Custom::Item.new("Bill")User.where(:name=>custom_item)有什么我可以在custom_item中定义的,所以它会理解Arel想要它的名字吗?目前我的解决方法是:moduleArelmoduleVisitorsclassToSqldefvisit_Custom_Itemo"'#{o.