rails-activerecord

Rails: Sort nils to the end of a scope?

送分小仙女□ 提交于 2019-12-18 13:09:04
问题 So, I have the following scope in my Photo model: scope :best, order(:average_rating.desc) The only problem is, the ratings were added to the model after the fact, so the production app has a lot of records where average_rating is nil. When I call this scope it returns all the nils first -- in fact it should be the opposite, nils should be last ( they are photos which have not yet been rated ). How can I sort nils to the end of this scope? 回答1: I'm a bit late to the game but this just came up

Rails ActiveRecord conditions

我只是一个虾纸丫 提交于 2019-12-18 12:35:16
问题 Is there a way to create a condition like this? @products = Product.find(:all, :limit => 5, :conditions => { :products => { :locale => 'en', :id NOT '1' }, :tags => { :name => ['a','b']}) I would like to list all products not including product 1. Thx. 回答1: Rails 3 Use squeel gem. Product.where( :products => { :locale => 'en', :id.not_in => '1' }, :tags => { :name => ['a','b']} ).limit(5) Rails 2 Use AR Extensions for this. It supports the following condition modifiers: * _lt => less than *

Ruby-on-Rails: Selecting distinct values from the model

余生长醉 提交于 2019-12-18 03:54:35
问题 The docs: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields Clearly state that: query = Client.select(:name).distinct # => Returns unique names However, when I try that in my controller, I get the following error: undefined method `distinct' for #<ActiveRecord::Relation:0xb2f6f2cc> To be clear, I want the distinct names, like ['George', 'Brandon'], not the clients actual records. Is there something that I am missing? 回答1: The .distinct option was added for

How to make ActiveRecord ThreadSafe

感情迁移 提交于 2019-12-18 03:43:30
问题 How can I make the following controller threadsafe in rails 4 with postgresql: def controller_action if Model.exists(column_name:"some_value") else @model=Model.new(column_name:"some_value") @model.save end end I am running puma, so my concern is that if two threads run this controller at the same time, and a row doesn't exist with the specified value of column_name, two records will be created whereas I only want 1. 回答1: Contrary to the comments, concurrent inserts on the same table are

Why alias_method fails in Rails model

不问归期 提交于 2019-12-18 01:52:21
问题 class Country < ActiveRecord::Base #alias_method :name, :langEN # here fails #alias_method :name=, :langEN= #attr_accessible :name def name; langEN end # here works end In first call alias_method fails with: NameError: undefined method `langEN' for class `Country' I mean it fails when I do for example Country.first . But in console I can call Country.first.langEN successfully, and see that second call also works. What am I missing? 回答1: ActiveRecord uses method_missing (AFAIK via ActiveModel:

I need help properly forming this ActiveRecord association

二次信任 提交于 2019-12-17 21:37:08
问题 Basically, a User can participate in one or more events as either (or potentially both) a vendor and as a member of the event's faculty. A user is, by default, categorized as neither a vendor nor a faculty member but rather attains either (or both) status(es) within the context of an event (e.g., the user has been admitted to an event as a member of its faculty). It seems like what I want to say is that the user has many events through either (or both) the vendors or the faculty linking

return custom query select in activerecord

佐手、 提交于 2019-12-17 19:22:19
问题 I've got a query that does some math and returns a calculated custom select field with the result set. I cannot figure out how to access that in the activerecord object that is returned. I've added an attr_accessor for it also. attr_accessor :percentage_used select('gateways.*, (num_transactions_today/ SUM(num_transactions_today)) AS percentage_used ').joins(:gateway_groups).where('map_gateway_groups.gateway_group_id = ?', gateway_group_id) in the result set, I would expect to have access to

ActiveRecord find and only return selected columns

梦想的初衷 提交于 2019-12-17 17:32:39
问题 edit 2 If you stumble across this, check both answers as I'd now use pluck for this I have a fairly large custom dataset that I'd like to return to be echoe'd out as json. One part is: l=Location.find(row.id) tmp[row.id]=l but I'd like to do something like: l=Location.find(row.id).select("name, website, city") tmp[row.id]=l but this doesn't seem to be working. How would I get this to work? thx edit 1 alternatively, is there a way that I can pass an array of only the attributes I want included

What is the difference between pluck and collect in Rails?

守給你的承諾、 提交于 2019-12-17 15:05:57
问题 Here are two sample codes. First one with collect : User.first.gifts.collect(&:id) Second one with pluck : User.first.gifts.pluck(:id) Is there any difference between them in performance or something else? 回答1: pluck is on the db level. It will only query the particular field. See this. When you do: User.first.gifts.collect(&:id) You have objects with all fields loaded and you simply get the id thanks to the method based on Enumerable. So: if you only need the id with Rails 4, use ids : User

Rails 3 - Eager loading with conditions

梦想的初衷 提交于 2019-12-17 12:34:23
问题 Okay, I'm thoroughly stumped on this one. I'm trying to build a menu of published web pages organized by category. Category.rb: belongs_to :parent, :class_name => "Category", :foreign_key => "parent_id" has_many :children, :class_name => "Category", :foreign_key => "parent_id" has_many :pages, :documents, :galleries Page.rb belongs_to :category The Page model also has :is_published, so I'm trying to filter on that as well. I am reluctant to post my feeble query attempts, but see no other