activerecord

Rails 3: How to find records with field possibly equals to nil?

浪尽此生 提交于 2020-01-03 03:00:32
问题 I would like to find all SongWriter s with maiden_name equals to my_maiden_name ? Note: my_maiden_name may be nil I tried: SongWriter.where("maiden_name = ?", my_maiden_name) and it works fine except the case where my_maiden_name = nil . When my_maiden_name = nil , the generated query is: SELECT `song_writers`.* FROM `song_writers` WHERE (maiden_name = NULL) rather than: SELECT `song_writers`.* FROM `song_writers` WHERE maiden_name IS NULL How could I generalize the active record query to

Converting a Postgres query to Rails ActiveRecord?

此生再无相见时 提交于 2020-01-03 02:00:08
问题 Is it possible to write the following query in Rail's ActiveRecord format? I've tried a billion different ways with arel but can't get the same results. SELECT topic_id, count(*) as total FROM questions_topics WHERE question_id IN ( SELECT id FROM questions WHERE user_id = 1000 UNION ALL SELECT questions.id FROM questions JOIN answers ON (questions.id = answers.question_id) WHERE answers.user_id = 1000 ) GROUP BY topic_id ORDER BY total DESC; 回答1: Well, this should work. Not exactly the same

Rails attributes headache

爱⌒轻易说出口 提交于 2020-01-02 20:10:21
问题 newbie rails question coming up. I have a class like this: class Thing < ActiveRecord::Base attr_accessible :name attr_accessor :name validates_uniqueness_of :name, :case_sensitive => false end I have done a migration and the table looks okay. I then fire up the rails console and try the following: t = Thing.new(:name => "test") => #<Thing id: nil, name: nil, description: nil, created_at: nil, updated_at: nil> already here it says name is nil, why? Continuing on, I try this: t.name => "test"

Lost connection to MySQL server during query on random simple queries

為{幸葍}努か 提交于 2020-01-02 10:09:49
问题 FINAL UPDATE: We fixed this problem by finding a way to accomplish our goals without forking. But forking was the cause of the problem. ---Original Post--- I'm running a ruby on rails stack, our mysql server is separate, but housed at the same site as our app servers. (we've tried swapping it out for a different mysql server with double the specs, but no improvement was seen. during business hours we get a handful of these from no particular query. ActiveRecord::StatementInvalid: Mysql2:

Lost connection to MySQL server during query on random simple queries

假装没事ソ 提交于 2020-01-02 10:09:02
问题 FINAL UPDATE: We fixed this problem by finding a way to accomplish our goals without forking. But forking was the cause of the problem. ---Original Post--- I'm running a ruby on rails stack, our mysql server is separate, but housed at the same site as our app servers. (we've tried swapping it out for a different mysql server with double the specs, but no improvement was seen. during business hours we get a handful of these from no particular query. ActiveRecord::StatementInvalid: Mysql2:

ActiveRecord and using reject method

点点圈 提交于 2020-01-02 10:02:28
问题 I have a model that fetches all the games from a particular city. When I get those games I want to filter them and I would like to use the reject method, but I'm running into an error I'm trying to understand. # STEP 1 - Model class Matches < ActiveRecord::Base def self.total_losses(cities) reject{ |a| cities.include?(a.winner) }.count end end # STEP 2 - Controller @games = Matches.find_matches_by("Toronto") # GOOD! - Returns ActiveRecord::Relation # STEP 3 - View cities = ["Toronto", "NYC"]

Finding unique records, ordered by field in association, with PostgreSQL and Rails 3?

混江龙づ霸主 提交于 2020-01-02 08:56:30
问题 UPDATE : So thanks to @Erwin Brandstetter, I now have this: def self.unique_users_by_company(company) users = User.arel_table cards = Card.arel_table users_columns = User.column_names.map { |col| users[col.to_sym] } cards_condition = cards[:company_id].eq(company.id). and(cards[:user_id].eq(users[:id])) User.joins(:cards).where(cards_condition).group(users_columns). order('min(cards.created_at)') end ... which seems to do exactly what I want. There are two shortcomings that I would still like

Rails 4.0.0 - Got “no implicit conversion of nil into String”

这一生的挚爱 提交于 2020-01-02 08:43:10
问题 Just installed a brand new Rails 4.0.0 app and I got this error (https://gist.github.com/hartator/6404820) on http://0.0.0.0:3000 : no implicit conversion of nil into String activerecord (4.0.0) lib/active_record/connection_adapters/sqlite3_adapter.rb:24:in `initialize' activerecord (4.0.0) lib/active_record/connection_adapters/sqlite3_adapter.rb:24:in `new' activerecord (4.0.0) lib/active_record/connection_adapters/sqlite3_adapter.rb:24:in `sqlite3_connection' activerecord (4.0.0) lib/active

Rails .reject Turns Type ActiveRecord::Relation into Type Array

大憨熊 提交于 2020-01-02 08:25:56
问题 I want to use the .reject method in Rails to remove the current_user from a search. I do this as follows: @users = User.where{(firstname =~ my{"%#{params[:term]}%"}) | (lastname =~ my{"%#{params[:term]}%"}) | (email =~ my{"%#{params[:term]}%"})}.select("id,firstname, lastname") @users = @users.reject{|u| User.find(u.id) == current_user} After the first line, if I call @users.class , I get ActiveRecord::Relation , but after using the .reject method, if I call @users.class I get Array . How can

How to override Rails' default migration generator template

徘徊边缘 提交于 2020-01-02 08:22:30
问题 I need to override these migration templates: https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb and https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb inside my rails application so that they pick up the template from rails application instead of the gem itself. I've