Commit Graph

4099 Commits

Author SHA1 Message Date
Jon Leighton
0096f53b25 nodoc the first_or_create methods and document alternatives 2012-10-19 15:56:18 +01:00
Jon Leighton
45d585e827 fix test :/ 2012-10-19 15:17:06 +01:00
Jon Leighton
af8c8b432a The default value of a text/blob in mysql strict mode should be nil
In non-strict mode it is '', but if someone is in strict mode then we
should honour the strict semantics.

Also, this removes the need for a completely horrible hack in dirty.rb.

Closes #7780
2012-10-19 15:08:58 +01:00
Jon Leighton
eb72e62c30 Add Relation#find_or_create_by and friends
This is similar to #first_or_create, but slightly different and a nicer
API. See the CHANGELOG/docs in the commit.

Fixes #7853
2012-10-19 13:18:47 +01:00
Aaron Patterson
506e26687d remove unused variables. Oops! 2012-10-17 15:55:30 -07:00
Aaron Patterson
e06c06d460 use columns hash to look up the column for the count field 2012-10-17 15:51:17 -07:00
Scott Willson
25262bc280 Fix bug with presence validation of associations.
Would incorrectly add duplicated errors when the association was blank. Bug introduced in 1fab518c6a75dac5773654646eb724a59741bc13.
2012-10-16 19:04:40 -07:00
Santiago Pastorino
c432c74cd3 Merge pull request #7371 from csmuc/fix_dup_validation_errors
Dup'ed ActiveRecord objects may not share the errors object
2012-10-16 09:46:44 -07:00
Christian Seiler
fb66521772 Call super to nullify the reference to the original errors object in the dup'ed object (call ActiveModel::Validations#initialize_dup). Closes #7291 2012-10-16 15:09:23 +02:00
Tim Macfarlane
51d6e21c96 ActiveRecord: sum expression returns string '0' for no records, fixed 2012-10-15 22:31:05 +01:00
Aaron Patterson
081f0ad413 use setup for setting up the test 2012-10-15 14:22:15 -07:00
Miguel Herranz
6f400dabf7 Fix typo in inet and cidr saving 2012-10-14 19:40:38 +02:00
Arturo Pie
2da85edda3 #7914 get default value when type uses schema name
PostgreSQL adapter properly parses default values when using multiple
schemas and domains.

When using domains across schemas, PostgresSQL prefixes the type of the
default value with the name of the schema where that type (or domain) is.

For example, this query:
```
SELECT a.attname, d.adsrc
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = "defaults"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
```

could return something like "'<default_value>'::pg_catalog.text" or
"(''<default_value>'::pg_catalog.text)::text" for the text columns with
defaults.

I modified the regexp used to parse this value so that it ignores
anything between ':: and \b(?:character varying|bpchar|text), and it
allows to have optional parens like in the above second example.
2012-10-13 19:41:50 -04:00
Tima Maslyuchenko
633ea6a826 learn ActiveRecord::QueryMethods#order work with hash arguments 2012-10-12 17:57:24 +03:00
Jon Leighton
9e5f7cc62e Revert "Key the attributes hash with symbols"
This reverts commit 86c3dfbd47cb96af02daaa655963292b1a1b110e.

Conflicts:
	activerecord/lib/active_record/attribute_methods/read.rb

Reason: whilst this increased performance, it also presents a DoS risk
via memory exhaustion if users were allowing user input to dictate the
arguments of read/write_attribute. I will investigate alternative ways
to cut down on string allocations here.
2012-10-12 11:49:13 +01:00
dfens
ab9140ff02 Cleanup trailing whitespaces 2012-10-12 09:56:39 +02:00
Vijay Dev
ecf460e7c4 Merge pull request #7887 from senny/remove_unused_requires_in_ar_tests
remove duplicated require statements in AR test cases
2012-10-10 13:42:33 -07:00
Vijay Dev
f638ef951b Merge branch 'master' of github.com:lifo/docrails
Conflicts:
	activerecord/lib/active_record/persistence.rb
	railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb
2012-10-11 01:05:14 +05:30
AvnerCohen
4c6f75c866 gradually moving documentation to new hash syntax 2012-10-10 10:54:31 +02:00
Aaron Patterson
269adaec21 Merge pull request #7859 from ernie/fix-collection-associations-with-select
Fix has_many assocation w/select load after create
2012-10-09 20:14:33 -07:00
Yves Senn
626f7df626 remove duplicated require statements in AR test cases 2012-10-09 21:33:54 +02:00
Alexey Muranov
c9ccef554c Move/rename files to follow naming conventions 2012-10-07 20:43:18 +02:00
Alexey Muranov
a94220b66c Rename "Fixtures" class to "FixtureSet"
Rename `ActiveRecord::Fixtures` class to `ActiveRecord::FixtureSet`.  Instances of this class normally hold a collection of fixtures (records) loaded either from a single YAML file, or from a file and a folder with the same name.  This change make the class name singular and makes the class easier to distinguish from the modules like `ActiveRecord::TestFixtures`, which operates on multiple fixture sets, or `DelegatingFixtures`, `::Fixtures`, etc., and from the class `ActiveRecord::Fixture`, which corresponds to a single fixture.
2012-10-07 19:15:58 +02:00
Rafael Mendonça França
86b13b6f54 Remove unneeded require 2012-10-06 19:31:57 -03:00
Rafael Mendonça França
5b91aa0018 Move multiparameter attributes related tests to its own file 2012-10-06 19:29:17 -03:00
Ernie Miller
9f3b8cd5a5 Fix has_many assocation w/select load after create
If you create a new record via a collection association proxy that has
not loaded its target, and which selects additional attributes through
the association, then when the proxy loads its target, it will
inadvertently trigger an ActiveModel::MissingAttributeError during
attribute writing when CollectionAssociation#merge_target_lists attempts
to do its thing, since the newly loaded records will possess attributes
the created record does not.

This error also raises a bogus/confusing deprecation warning when
accessing the association in Rails 3.2.x, so cherry-pick would be
appreciated!
2012-10-05 18:36:44 -04:00
Santiago Pastorino
a0613ad8a9 Revert "Use flat_map { } instead of map {}.flatten"
This reverts commit abf8de85519141496a6773310964ec03f6106f3f.
We should take a deeper look to those cases flat_map doesn't do deep
flattening.

irb(main):002:0> [[[1,3], [1,2]]].map{|i| i}.flatten
=> [1, 3, 1, 2]
irb(main):003:0> [[[1,3], [1,2]]].flat_map{|i| i}
=> [[1, 3], [1, 2]]
2012-10-05 12:07:16 -03:00
Santiago Pastorino
abf8de8551 Use flat_map { } instead of map {}.flatten 2012-10-05 11:52:20 -03:00
Yves Senn
16e4a53b78 PostgreSQL, quote table names when fetching the primary key. Closes #5920 2012-10-05 08:54:38 +02:00
Francesco Rodriguez
aa202adf6c Count returns 0 without querying if parent is not saved
Patches `CollectionAssociation#count` to return 0 without querying
if the parent record is new. Consider the following code:

    class Account
      has_many :dossiers
    end

    class Dossier
      belongs_to :account
    end

    a = Account.new
    a.dossiers.build

    # before patch
    a.dossiers.count
    # SELECT COUNT(*) FROM "dossiers" WHERE "dossiers"."account_id" IS NULL
    # => 0

    # after
    a.dosiers.count # fires without sql query
    # => 0

Fixes #1856.
2012-10-03 18:02:14 -05:00
kennyj
df08271f9c fix warning: method redefined 2012-10-02 13:32:54 -03:00
lulalala
6e56a03f83 Fix reset_counters() crashing on has_many :through associations.
The counter column name in the intermediate model need to be access
via the through reflection.
2012-10-02 23:49:32 +08:00
Yasuo Honda
10f5f2305d Change query pattern case insensitive
because Oracle adapter uses upper case attribute/column name.
2012-09-29 04:57:27 +09:00
Jon Leighton
d14da79334 Add an explicit test for hot compatibility 2012-09-28 18:08:14 +01:00
Jon Leighton
144e8691cb Support for partial inserts.
When inserting new records, only the fields which have been changed
from the defaults will actually be included in the INSERT statement.
The other fields will be populated by the database.

This is more efficient, and also means that it will be safe to
remove database columns without getting subsequent errors in running
app processes (so long as the code in those processes doesn't
contain any references to the removed column).
2012-09-28 18:08:14 +01:00
Jeremy Kemper
d665f91d41 Fix destructive side effects from marshaling an association caused by 65843e1acc0c8d285ff79f8c9c49d4d1215440be 2012-09-25 13:20:35 -07:00
Yasuo Honda
5826b5008f Skip tests for non-supported isolation levels
i.e. Oracle database does not support these isolation levels.
`:read_uncommitted` `:repeatable_read`

This commit also works with other databases which do not support
these isolation levels.
2012-09-26 04:29:25 +09:00
Yasuo Honda
5f1710b187 Skip tests for non-supported isolation levels with Oracle 2012-09-25 23:42:54 +09:00
John Foley
d66d6076b2 Fix collisions with before and after validation callbacks.
This commit allows a user to do something like:
    before_validation :do_stuff, :on => [ :create, :update ]
    after_validation :do_more, :on => [ :create, :update ]
2012-09-23 12:53:50 -06:00
Santiago Pastorino
19fa8fa3c4 Fix test_find_in_batches_should_use_any_column_as_primary_key 2012-09-22 10:57:54 -03:00
Santiago Pastorino
5b99cf088c start could be a string
Related to 761bc751d31c22e2c2fdae2b4cdd435b68b6d783 and
eb876c4d07130f15be2cac7be968cc393f959c62
2012-09-21 19:42:16 -03:00
Santiago Pastorino
eb876c4d07 Revert "Fix find_in_batches with customized primary_key"
This reverts commit 761bc751d31c22e2c2fdae2b4cdd435b68b6d783.

This commit wasn't fixing any issue just using the same table for
different models with different primary keys.
2012-09-21 19:14:17 -03:00
Jeremy Kemper
7174307bd8 Style: remove hash noise 2012-09-21 12:08:02 -07:00
Jeremy Kemper
78b30fed93 Correct default charset/collation for mysql dbs 2012-09-21 12:03:16 -07:00
Jon Leighton
f74ddc8a4c Make the serializable test much looser
It's too hard to test this properly, so let's just check that there are
no errors.
2012-09-21 17:56:04 +01:00
Jon Leighton
392eeecc11 Support for specifying transaction isolation level
If your database supports setting the isolation level for a transaction,
you can set it like so:

  Post.transaction(isolation: :serializable) do
    # ...
  end

Valid isolation levels are:

* `:read_uncommitted`
* `:read_committed`
* `:repeatable_read`
* `:serializable`

You should consult the documentation for your database to understand the
semantics of these different levels:

* http://www.postgresql.org/docs/9.1/static/transaction-iso.html
* https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html

An `ActiveRecord::TransactionIsolationError` will be raised if:

* The adapter does not support setting the isolation level
* You are joining an existing open transaction
* You are creating a nested (savepoint) transaction

The mysql, mysql2 and postgresql adapters support setting the
transaction isolation level. However, support is disabled for mysql
versions below 5, because they are affected by a bug
(http://bugs.mysql.com/bug.php?id=39170) which means the isolation level
gets persisted outside the transaction.
2012-09-21 16:32:27 +01:00
Jon Leighton
834d6da54e Merge pull request #5248 from jcoleman/should-unset-association-when-an-existing-record-is-destroyed
Unset association when existing record is destroyed.
2012-09-21 02:40:26 -07:00
Carlos Antonio da Silva
a507c641ec Get rid of global variable in AR transactions test 2012-09-20 23:14:35 -03:00
Francesco Rodriguez
a30b8d38b4 rename AR::Model::Tag to AR::Tag - fixes #7714 2012-09-20 12:43:12 -05:00
Damien Mathieu
30a576fa14 fix querying with an empty hash
Closes #6960
2012-09-19 15:57:22 +02:00