activerecord

How to override Rails' default migration generator template

。_饼干妹妹 提交于 2020-01-02 08:22:17
问题 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

Calling ActiveRecord's #relationship_ids = [1,2,3] saves immediately. Any workarounds?

偶尔善良 提交于 2020-01-02 08:21:38
问题 I've come across an oddity in ActiveRecord's #relationship_ids method (that's added automatically when you declare 'has_many'), which saves immediately for existing records, which is causing me some issues, and I wonder if anyone had any useful advice. I'm running Rails 2.3.5. Consider this simple scenario, where an article has_many tags, say: a = Article.first a.name = "New Name" # No save yet a.author_id = 1 # No save yet a.tag_ids = [1,2,3] # These changes are saved to the database #

Calling ActiveRecord's #relationship_ids = [1,2,3] saves immediately. Any workarounds?

允我心安 提交于 2020-01-02 08:21:12
问题 I've come across an oddity in ActiveRecord's #relationship_ids method (that's added automatically when you declare 'has_many'), which saves immediately for existing records, which is causing me some issues, and I wonder if anyone had any useful advice. I'm running Rails 2.3.5. Consider this simple scenario, where an article has_many tags, say: a = Article.first a.name = "New Name" # No save yet a.author_id = 1 # No save yet a.tag_ids = [1,2,3] # These changes are saved to the database #

Retrieving a single record from an ActiveRecord/Rails query

不问归期 提交于 2020-01-02 07:48:08
问题 I issue queries like the following that retrieve exactly 0 or 1 records: car = Car.where(vin: '1234567890abcdefg') What's returned is of course a list of cars of length 1. So I end up adding .first at the end of the query: car = Car.where(vin: '1234567890abcdefg').first Seems like there should be a better way in Rails. Does something like .onlyOne exist which would return just a single Car and throw an exception if there was more than 1? car = Car.where(vin: '1234567890abcdefg').onlyOne or

rails attribute names camelcase issue

 ̄綄美尐妖づ 提交于 2020-01-02 07:32:11
问题 I have legacy Sql Server database and Rails applications. Column names in database are all in PascalCase (FirstName, CustomerId...). I'm searching for an easy way to use underscore_casing in Ruby and PascalCasing in database. I would like to write first_name in Rails for FirstName column in database. camelize and underscore will convert a string to required casing but I'm looking for something more general like: ActiveRecord::Base.camelize_table_column_names = true like existing ActiveRecord:

ActiveRecord query for a count of new users by day

橙三吉。 提交于 2020-01-02 07:22:07
问题 I want to generate a table showing how many users were created each day. What's the most efficient or elegant ActiveRecord query to do this? My (simplified) schema looks like this: create_table "users" do |t| t.datetime "created_at" t.string "name" end I want a table that looks like this, where the integers are a count of new users each day: day new users 2012-04-01 55 2012-04-02 63 2012-04-03 77 2012-04-04 88 I want to generate the table from an array that looks like this: data_table.add

Help with a Join in Rails 3

こ雲淡風輕ζ 提交于 2020-01-02 07:13:18
问题 I have the following models: class Event < ActiveRecord::Base has_many :action_items end class ActionItem < ActiveRecord::Base belongs_to :event belongs_to :action_item_type end class ActionItemType < ActiveRecord::Base has_many :action_items end And what I want to do is, for a given event, find all the action items that have an action item type with a name of "foo" (for example). So I think the SQL would go something like this: SELECT * FROM action_items a INNER JOIN action_item_types t ON a

How to find size of query result

不羁岁月 提交于 2020-01-02 07:04:11
问题 I have the following query in rails: records = Record.select('y_id, source') .where(:source => source, :y_id => y_id) .group(:y_id, :source) .having('count(*) = 1') I get the following output if I puts records : [#<Record source: "XYZ", y_id: 10000009>, #<Record source: "XYZ", y_id: 10000070>] This looks like there are 2 elements in the output array. But when I try to do records.size I get: {[10000009, "XYZ"]=>1, [10000070, "XYZ"]=>1} Why doesn't records.size print 2 when records is an array

How to find size of query result

别等时光非礼了梦想. 提交于 2020-01-02 07:04:05
问题 I have the following query in rails: records = Record.select('y_id, source') .where(:source => source, :y_id => y_id) .group(:y_id, :source) .having('count(*) = 1') I get the following output if I puts records : [#<Record source: "XYZ", y_id: 10000009>, #<Record source: "XYZ", y_id: 10000070>] This looks like there are 2 elements in the output array. But when I try to do records.size I get: {[10000009, "XYZ"]=>1, [10000070, "XYZ"]=>1} Why doesn't records.size print 2 when records is an array

Rolling back Transactions Without using a Block

半腔热情 提交于 2020-01-02 05:37:05
问题 Note: This is a follow up question to Optimising Rspec Tests to Avoid Repeating Complex Setup Proceedures For reasons that are outside the scope of this question (see the note above), I want to be able to start a Rails database transaction and then rollback that transaction in a different scope. E.g: def before_callback start_transaction # Start the transaction # Create/Update some records end def after_callback rollback_transaction # Rollback changes from before_callback and do_stuff end def