arel

Best way to find a single record using ActiveRecord 3 / Arel?

蹲街弑〆低调 提交于 2019-12-03 22:04:04
Where I used to do this: Foo.find_by_bar('a-value') I can now do this: Foo.where(:bar => 'a-value').limit(1).first Is this recommended? Is this the best way? Should I continue to use the "old" way because it continues to be useful syntactic sugar, or is there an Even Better way I can do that now, which will support chaining and all the other good stuff? SureshCS Rails 4 : Foo.find_by bar: 'a_value' , wibble: 'a wibble value' I think the preferable way to return a single record would be along the lines of your second example, but you can omit the limit part: Foo.where(:bar => 'a-value').first

Ransack: How to use existing scope?

末鹿安然 提交于 2019-12-03 16:14:12
问题 Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic. Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example: class Post < ActiveRecord::Base scope :year, lambda { |year| where("posts.date BETWEEN '#{year}-01-01' AND '#{year}-12-31'") } end So far as I know, this can be achieved by defining a custom ransacker. Sadly, I don't find any documentation about this. I tried this in the Post class: ransacker :year,

Rails `where` for time less than queries

老子叫甜甜 提交于 2019-12-03 15:56:06
问题 Setup Rails' where method can take a range in a hash to generate a query that will search for a value that is within the range. For example: User.where(cash_money: 10..1000) #=> SELECT `users`.* FROM `users` WHERE (`users`.`cash_money` BETWEEN 10 AND 1000) This can also be used with timestamps like User.where(last_deposit: 10.days.ago..1000.days.ago) #=> SELECT `users`.* FROM `users` WHERE (`users`.`last_deposit` BETWEEN '2014-05-19 14:42:36' AND '2011-09-02 14:42:36') I've found that you can

Concatenate (glue) where conditions by OR or AND (Arel, Rails3)

眉间皱痕 提交于 2019-12-03 15:38:32
I have several complex queries (using subqueries, etc...) and want to glue them together with OR or AND statement. For example: where1=table.where(...) where2=table.where(...) I would like something like where3=where1.or where2 Next example doesn't work for me: users.where(users[:name].eq('bob').or(users[:age].lt(25))) because of I have several where(..) queries and I want to concatenate them . In other words I have 3 methods: first return first where, second-second, third - OR concatenation. I must have able to use all 3 methods in my application and save DRY code are you looking for the form

problem: activerecord (rails3), chaining scopes with includes

好久不见. 提交于 2019-12-03 15:00:42
In Rails3 there seems to be a problem when chaining two scopes (ActiveRelations) that each have a different include: Consider these two scopes, both of which work fine on their own: First scope: scope :global_only, lambda { |user| includes(:country) .where("countries.area_id <> ?", user.area) } Work.global_only(user) => (cut list of fields from SQL for legibility) SELECT * FROM "works" LEFT OUTER JOIN "countries" ON "countries"."id" = "works"."country_id" WHERE (countries.area_id <> 3) Now the second scope: scope :not_belonging_to, lambda { |user| includes(:participants) .where("participants

Not case sensitive search with active record

依然范特西╮ 提交于 2019-12-03 13:27:23
问题 I use rails 3.0.4 here is a piece of Arel query in my rails application, How can I use the upcase method to make a none case sensitive search in a database agnostic way? Customer.where("company_id = ? and (firstname like ? or lastname like ? or reference like ?)", current_user.company_id, "%#{params[:query]}%", "%#{params[:query]}%", "%#{params[:query]}%") Thanks 回答1: Here are a couple of options for you. First, LIKE is already case-insensitive, but for Postgres you'll have to use ILIKE to

How to do joins on subqueries in AREL within Rails

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 12:58:13
I have a simple model class User has_many :logs class Logs related in the usual way through the foreign key logs.user_id. I'm trying to do the following using Arel and according to the Arel doc it should work. u_t = Arel::Table::new :users l_t = Arel::Table::new :logs counts = l_t. group(l_t[:user_id]). project( l_t[:user_id].as("user_id"), l_t[:user_id].count.as("count_all") ) l_t.joins(counts).on(l_t[:id].eq(counts[:user_id])) When I do that I get the error TypeError: Cannot visit Arel::SelectManager However the author of Arel explicitly suggests that Arel can do this kind of thing. Please

Arel: How to cleanly join multiple conditions with OR?

故事扮演 提交于 2019-12-03 12:29:46
问题 In my Rails app, I loop through an array to create a list of conditions that must be joined by OR. Below is the basic flow of how I currently do so. conditions = nil set.each do |value| condition = value.to_condition conditions = conditions ? conditions.or(condition) : condition end Obviously, it's not beautiful, but I still don't fully know my way around Arel. Does it offer any better way of OR-joining a set of dynamically-generated conditions? 回答1: This is a perfect fit for an inject which

Rails 4 query unique by single attribute

给你一囗甜甜゛ 提交于 2019-12-03 08:38:06
问题 So this is more of an arel question than anything but here's what I am trying to do. I have three objects lets say, called Items <Item id: 1, name: 'Book'> <Item id: 2, name: 'Car'> <Item id: 3, name: 'Book'> I want to do a query that will just return only one of each unique "name" attributes. Something like Item.select('distinct(name), items.*') This doesn't work though, it still returns all three items. How can I form this query so that it only returns: <Item id: 1, name: 'Book'> <Item id:

How to make attribute setter send value through SQL function

偶尔善良 提交于 2019-12-03 07:59:48
I'm trying to make an attribute setter in an ActiveRecord model wrap its value in the text2ltree() postgres function before rails generates its sql query. For example, post.path = "1.2.3" post.save Should generate something like UPDATE posts SET PATH=text2ltree('1.2.3') WHERE id = 123 # or whatever What's the best way of doing this? EDIT: To achieve exactly what you are looking for above, you'd use this to override the default setter in your model file: def path=(value) self[:path] = connection.execute("SELECT text2ltree('#{value}');")[0][0] end Then the code you have above works. I'm