activerecord

ActiveRecord query with multiple joins not recognizing the relations

天大地大妈咪最大 提交于 2019-12-24 01:24:37
问题 I am trying to write an ActiveRecord Query that returns all students enrolled in a certain course with the following query: def self.students_enrolled_in(course_id) Student .joins(:enrollments) .joins(:sections) .joins(:courses) .where(sections: { course_id: course_id }) end the result in the rails console is: ActiveRecord::ConfigurationError: Can't join 'Student' to association named 'sections'; perhaps you misspelled it? it seems that the association is made. what am I doing wrong? does the

Rails 3 ActiveRecord sum of a model for each associated model

懵懂的女人 提交于 2019-12-24 00:17:11
问题 I have 2 models Category - id - name and Transaction - id - category_id - amount I want to find the sum of all transactions for each category. I know I can get a list of caterogies and then get the sum for all the transactions with the category_id but it will do 20+ queries. Is there a way to do it all in one query? Edit: I want to end up with a list of [[category1, sum], [category2, sum]]. 回答1: Transaction.group(:category_id).sum(:amount) This will return a hash similar to this: {CATEGORY_ID

Disjunction in ActiveRecord

拜拜、爱过 提交于 2019-12-24 00:11:48
问题 Is it possible to use ActiveRecord named_scope s to create one query with sql OR clauses? When I use Model.scope1.scope2 generated query is conjunction of these scopes. 回答1: This isn't really what named scopes were designed to do, but you could probably use them with some additional code to get what you needed. def combine_scopes(model) (model.scope1 + model.scope2).uniq end or allow any scopes to be combined def combine_scopes(model, scope1, scope2) (model.send(scope1) + model.send(scope2))

find_by_sql renders an array

混江龙づ霸主 提交于 2019-12-23 23:05:18
问题 I got some problems here, I can't make my find_by_sql request to render an ActiveRecord relation. Indeed, I need an activerecord relation to make a new request: @searches = @searches.find_by_sql('SELECT *, COUNT( follower_id ) FROM follows GROUP BY followable_id LIMIT 0 , 3') if params[:only_famous_projects] @project_pages = @project_pages.where(:project_id => @searches.pluck(:'followable.id')) if params[:only_famous_projects] I can't use "pluck" without an activerecord relation. Therefore, I

Rails 3 model mapping certain columns to different model attributes

久未见 提交于 2019-12-23 21:38:47
问题 I have the old legacy table called "DXFTACCTS", and I created Rails model "Account". class Account < ActiveRecord::Base set_table_name "DXFTACCTS" end The problem is that DXFTACCTS has fields like "XORFNAME" which I want to be "first_name" in the model, and so on. How do I "map" specific table columns to model attributes? Thanks! 回答1: You can use the method alias_attribute like this: class Account < ActiveRecord::Base set_table_name "DXFTACCTS" alias_attribute :first_name, :XORFNAME end alias

MySQL, Rails ActiveRecord date grouping and timezones

老子叫甜甜 提交于 2019-12-23 21:03:16
问题 I want to count users by creation date. When I query my last user, I have: > User.last.created_at => Thu, 07 Aug 2014 21:37:55 BRT -03:00 When I count users per date I get this: > User.group("date(created_at)").count => {Fri, 08 Aug 2014=>1} The creation date is Aug 7, but the result is Aug 8. This is happening because the group condition is in UTC and my timezone is 'Brasilia'. I have this in my application.rb : config.time_zone = 'Brasilia' config.active_record.default_timezone = :local How

Rails bug: accepts_nested_attributes_for is not updating my has_many association

て烟熏妆下的殇ゞ 提交于 2019-12-23 21:01:37
问题 I have 2 models: class Book < ActiveRecord::Base has_many :book_versions accepts_nested_attributes_for :book_versions, allow_destroy: true validates_associated :book_versions class BookVersion < ActiveRecord::Base has_many :collection_items has_many :collections, through: :collection_items belongs_to :book validates_presence_of :price, :isbn #<-- validates presence Here are my params. Notice how I leave the price of book_version with name bb blank. This should be firing off the validates

Devise with existing database: 401 Unauthorized using valid password

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 20:00:14
问题 I am trying to use devise for user authentication of my website. It uses an existing User table. The table has username and password, however when I try to log in, I get the following error: Completed 401 Unauthorized in 90ms. Logs: Started POST "/users/sign_in" for 127.0.0.1 at 2013-06-06 15:50:01 +0530 Processing by Devise::SessionsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"+uKE28LrkWW71EHeasDSDCENQogAGODfBc3ZJJLyi9U=", "user"=>{"email"=>"admin@abc.com",

actionwebservice and rails 3 /

非 Y 不嫁゛ 提交于 2019-12-23 19:55:37
问题 I'm on a rails 3 project trying to install actionwebservice using: gem 'rails', '3.0.4' gem 'actionwebservice', :git => 'https://github.com/ywen/actionwebservice.git' And I get the error: Bundler could not find compatible versions for gem "activerecord": In Gemfile: actionwebservice depends on activerecord (= 2.3.5) rails (= 3.0.4) depends on activerecord (3.0.4) Any suggestions? 回答1: I had the same problem. Try this gem: https://github.com/dnordberg/actionwebservice gem 'actionwebservice',

Rails 2.3: How to turn this SQL statement into a named_scope

左心房为你撑大大i 提交于 2019-12-23 19:12:32
问题 Having a bit of difficulty figuring out how to create a named_scope from this SQL query: select * from foo where id NOT IN (select foo_id from bar) AND foo.category = ? ORDER BY RAND() LIMIT 1; Category should be variable to change. What's the most efficient way the named_scope can be written for the problem above? 回答1: named_scope :scope_name, lambda { |category| { :conditions => ["id NOT IN (select foo_id from bar) AND foo.category = ?", category], :order => 'RAND()', :limit => 1 } } 回答2: