Commit Graph

520 Commits

Author SHA1 Message Date
John Wang
455d710242 If a counter_cache is defined, then using update_attributes and changing
the primary key on an association will make sure that the corresponding
counter on the association is changed properly. Fixes #9722.
2013-03-15 21:28:28 -04:00
Yves Senn
34402c7623 make it possible to disable implicit join references.
Closes #9712.
2013-03-15 16:07:05 +01:00
Yves Senn
cd87c85ef0 Deprecate the :distinct option for Relation#count.
We moved more and more away from passing options to finder / calculation
methods. The `:distinct` option in `#count` was one of the remaining places.
Since we can now combine `Relation#distinct` with `Relation#count` the option
is no longer necessary and can be deprecated.
2013-03-15 14:15:47 +01:00
Yves Senn
a1bb6c8b06 rename Relation#uniq to Relation#distinct. #uniq still works.
The similarity of `Relation#uniq` to `Array#uniq` is confusing. Since our
Relation API is close to SQL terms I renamed `#uniq` to `#distinct`.

There is no deprecation. `#uniq` and `#uniq!` are aliases and will continue
to work. I also updated the documentation to promote the use of `#distinct`.
2013-03-15 14:15:47 +01:00
Adam Gamble
8fccbc1ad4 Modifies belong_to touch callback to touch old associations also #9091 2013-03-14 12:46:50 -05:00
Yves Senn
5ed4696b67 fix typo in Active Record CHANGELOG [ci skip] 2013-03-13 09:11:48 +01:00
Matthew M. Boedicker
d3e5118e7d Pass column to quote when copying a sqlite table.
To make quote escape binary data correctly it needs the column passed in.
2013-03-11 23:14:46 -07:00
Xavier Noria
cae93be0ca promotes change_column_null to the migrations API 2013-03-11 23:13:12 +01:00
Carlos Antonio da Silva
bbc87dbc81 Merge pull request #9633 from senny/5321_make_it_lazy
Uniqueness validation uses a proc to specify the `:conditions` option.

This is a follow up to #5321 and follows the general direction in
AR to make things lazy evaluated.
2013-03-09 14:11:21 -08:00
Yves Senn
ad1a24f0e0 Uniqueness validation uses a proc to specify the :conditions option.
This is a follow up to #5321 and follows the general direction in
AR to make things lazy evaluated.
2013-03-09 23:09:00 +01:00
Carlos Antonio da Silva
a6bd507ece Fix "ActiveRecord" => "Active Record" in changelog [ci skip] 2013-03-09 18:22:47 -03:00
Ben Moss
992d87db02 Deprecate #connection in favour of accessing it via the class
This allows end-users to have a `connection` method on their models
without clashing with ActiveRecord internals.
2013-03-09 15:16:15 -05:00
Yves Senn
a58e660837 Use "Fixes" in place of "Fix" in changelogs for consistency [ci skip]. 2013-03-09 20:57:17 +01:00
OZAWA Sakuro
c3a26c592c Preserve magic comments and content encoding of copied migrations.
During insertion of "# This migration comes from ... " comment at the beginning of
a migration, presence of magic comment was not considered.
2013-03-09 15:38:02 +09:00
Carlos Antonio da Silva
a2d57fd6b9 Fix changelog indent, remove self from method call in changelog/doc examples
[ci skip]
2013-03-08 12:35:45 -03:00
Rafael Mendonça França
74d24ea1fe Merge pull request #9497 from route/subclass_from_attrs
Fix ActiveRecord `subclass_from_attrs` when eager_load is false.

Conflicts:
	activerecord/CHANGELOG.md
2013-03-08 11:59:29 -03:00
Rafael Mendonça França
ba6b3c165f Merge pull request #8868 from tehgeekmeister/master
Use the index name explicitly provided in a migration when reverting.

Fixes #8868

Conflicts:
	activerecord/CHANGELOG.md
2013-03-08 10:35:16 -03:00
Rafael Mendonça França
22f31214a0 Merge pull request #8966 from cfabianski/disable_prepared_statement_when_preparing_a_query
Unprepared Visitor + unprepared_statement

Conflicts:
	activerecord/CHANGELOG.md
2013-03-08 10:19:58 -03:00
Jon Leighton
9f007d7fe5 Write a more comprehensive CHANGELOG message [ci skip] 2013-03-08 10:44:42 +00:00
Cédric FABIANSKI
9f549212c3 Unprepared Visitor + unprepared_statement 2013-03-08 09:14:31 +01:00
Ezekiel Smithburg
b6226c3cfb If an index can't be found by column, use the index name.
schema_statements uses the column name by default to construct the index name, and then raises an exception if it doesn't exist, even if the name option is specified, which causes #8858.  this commit makes index_name_for_remove fall back to constructing the index name to remove based on the name option.
2013-03-07 20:03:17 -08:00
Neeraj Singh and John Leighton
cd26b6ae7c Combine scope conditions using AND
Currently Post.active.inactive will result in Post.inactive since
the last where clause wins when scopes are merged.

This pull request will merge all scopes ( barring defaul scope)
using AND.

The default scope will be overridden if another scope acts on the
same where clause.

closes #7365
2013-03-07 18:57:27 -05:00
Rafael Mendonça França
2e3e171e31 Merge pull request #9105 from bemurphy/cache_key_updated_on
cache_key consults updated_on timestamp if present

Conflicts:
	activerecord/CHANGELOG.md
2013-03-07 13:28:46 -03:00
Rafael Mendonça França
d3adfd6d3b Merge pull request #9474 from HonoreDB/master
More helpful error message when instantiating an abstract class

Conflicts:
	activerecord/CHANGELOG.md
2013-03-07 13:26:03 -03:00
Yves Senn
0a5fdcd5ae rake db:structure:dump warns when mysqldump is not in PATH.
Closes #9518.

The rake task used to fail silently and left an empty `structure.sql`.
It's confusing for users to get to the root of the problem.
The warning message tells them where to look.
2013-03-06 15:12:48 +01:00
Yves Senn
854b74242c Connection#structure_dump is no longer used. #9518
As of ccc6910c we use `mysqldump` to create the `structure.sql`.
The old `#structure_dump` code is still in AR but never used.

I removed all relevant parts from the code-base.
2013-03-06 15:12:20 +01:00
Dmitry Vorotilin
b04051d4e0 Fix ActiveRecord subclass_from_attrs when eager_load is false.
It cannot find subclass because all classes are loaded automatically
when it needs.
2013-03-06 11:46:07 +04:00
Yves Senn
b337390889 transactions can be turned off per Migration.
Closes #9483.

There are SQL Queries that can't run inside a transaction. Since
the Migrator used to wrap all Migrations inside a transaction there
was no way to run these queries within a migration.

This patch adds `self.disable_ddl_transaction!` to the migration to
turn transactions off when necessary.
2013-03-05 16:12:08 +01:00
Yves Senn
4b7a33e142 assigning '0.0' to a nullable numeric column does not make it dirty 2013-03-05 14:00:41 +01:00
Dimko
71e4dfc9f4 Fixed typo in active record's changelog 2013-03-04 18:56:46 +04:00
Carlos Antonio da Silva
96050e1d73 Use 1.9 hash style in changelog and doc examples [ci skip] 2013-03-04 09:04:48 -03:00
wangjohn
293875457b Created an unscope method for removing relations from a chain of
relations. Specific where values can be unscoped, and the unscope method
still works when relations are merged or combined.
2013-03-03 20:42:01 -05:00
Aaron Weiner
53f18f2c54 More helpful error message when instantiating an abstract class
Calling a literal ActiveRecord::Base.new raises NoMethodError,
since it ends up calling Class.abstract_class? which does not exist.
Similarly, instantiating an actual abstract class hits the database,
when conventionally it should immediately throw NotImplementedError.

ActiveRecord::Base can't be made abstract without breaking many,
many things, so check for it separately.
2013-03-03 12:36:51 -05:00
Andrew White
c09f934dcb Merge pull request #9431 from troyk/patch-2
Fix PostgreSQL TIMESTAMP WITH TIME ZONE to return ActiveSupport::Time
2013-03-02 10:03:04 +00:00
Yves Senn
b9399c470b deal with #append and #prepend on association collections.
Closes #7364.

Collection associations behave similar to Arrays. However there is no
way to prepend records. And to append one should use `<<`. Before this
patch `#append` and `#prepend` did not add the record to the loaded
association.

`#append` now behaves like `<<` and `#prepend` is not defined.
2013-03-01 21:29:46 +01:00
Troy Kruthoff
2cc09441c2 Fix PostgreSQL TIMESTAMP WITH TIME ZONE to return ActiveSupport::Time
In an AR model a timestamptz attribute would return a ruby string and AR
tests did not check for any type casting.  Previous tests would pass
only because an assert_equal was being used on a Time.utc object, which
will parse the right side of the eq to a valid Time instance for
comparision.

switch to test instance of Time instead of ActiveSupport::TimeWithZone
2013-03-01 09:51:23 -08:00
Sammy Larbi
20e041579f Support creating a table migration generator
Sometimes you want to create a table without an associated model and
test, which is also not a join table. With this commit, you can now
do that.

Example:

    rails g migration create_posts title:string
or
    rails g migration CreatePosts title:string

This commit also moves the template the model generator uses for the
migration to the migration templates folder, as it seems a more
sensible place for it now that it is shared code.
2013-03-01 06:13:30 -06:00
Rafael Mendonça França
2c3362829f Improve the CHANGELOG entry
[ci skip]
2013-02-28 18:35:07 -03:00
kennyj
e2a4b7a506 Wrong exception is occured when raising no translatable exception 2013-03-01 02:47:57 +09:00
Yves Senn
5d0ca74622 Support PostgreSQL specific column types when using change_table.
Closes #9480.

We use `TableDefinition` for `#create_table` and `Table` for `#change_table`.
The PostgreSQL sepcifc types were only defined on `TableDefinition` so I
also added them to `Table`.
2013-02-28 13:42:30 +01:00
Steve Klabnik
afd4a14332 Revert "Merge pull request #9207 from dylanahsmith/mysql-quote-numeric"
This reverts commit 408227d9c5ed7de26310d72a1a99c1ee02311c63, reversing
changes made to dca0b57d03deffc933763482e615c3cf0b9a1d97.
2013-02-27 08:46:40 -08:00
Carlos Antonio da Silva
02037c3ea0 Merge pull request #9218 from Fivell/master
Fix calculation of db_runtime property in
ActiveRecord::Railties::ControllerRuntime#cleanup_view_runtime.

Previously, after raising ActionView::MissingTemplate, db_runtime was
not populated.

Closes #9218, Fixes #9215.

Conflicts:
	activerecord/CHANGELOG.md
2013-02-27 11:47:24 -03:00
Igor
dd0bbd2ccb fix db_runtime attribute value after raising ActionView::MissingTemplate exception 2013-02-27 16:35:22 +02:00
Olek Janiszewski
dce4383319 Fix touching an invalid parent record for belongs_to
If the parent of a `belongs_to` record fails to be saved due to
validation errors, `touch` will be called on a new record, which causes
an exception (see https://github.com/rails/rails/pull/9320).

Example:

    class Owner < ActiveRecord::Base
      validates_presence_of :name
    end

    class Pet < ActiveRecord::Base
      belongs_to :owner, touch: true
    end

    pet = Pet.new(owner: Owner.new)

    # Before, this line would raise ActiveRecord::ActiveRecordError
    # "can not touch on a new record object"
    pet.save
2013-02-26 18:46:10 +01:00
Rafael Mendonça França
0f75e2c51f Update CHANGELOGS 2013-02-26 13:45:57 -03:00
Rafael Mendonça França
8bc5e714f4 Merge pull request #9414 from senny/9275_order_with_symbol_and_join
Expand order(:symbol) to "table".symbol to prevent broken queries on PG.
2013-02-26 05:15:14 -08:00
Brendon Murphy
1dc98c143c cache_key consults updated_on timestamp if present
- Extract max timestamp retrieval for cache_key
- Update changelog for cache_key changes
2013-02-26 00:04:05 -08:00
Xavier Noria
25f90a4140 typo 2013-02-25 21:31:54 +01:00
Xavier Noria
3d303f5af8 AR loads activerecord-deprecated_finders, no need to add it to the Gemfile 2013-02-25 21:31:03 +01:00
Yves Senn
e035699085 Expand order(:symbol) to "table".symbol to prevent broken queries on PG.
Fixes #9275.

When `#order` is called with a Symbol this patch will prepend the quoted_table_name.
Before the postgresql adapter failed to build queries containg a join and an order
with a symbol.

This expansion happens for all adapters.
2013-02-25 18:00:34 +01:00
Rafael Mendonça França
d10056087c New CHANGELOG entries always on top [ci skip] 2013-02-25 12:57:19 -03:00
Hiroshige Umino
ec9aa3ca89 Do not override attributes on dup by default scopes 2013-02-26 00:30:49 +09:00
David Heinemeier Hansson
c0bc9ce38c Preparing for 4.0.0.beta1 release 2013-02-25 08:31:50 -06:00
Yves Senn
536b3f0fb5 some minor AR changelog modifications [ci skip] 2013-02-25 11:46:05 +01:00
wangjohn
e52ff80960 Raising an ActiveRecordError when one tries to use .touch(name) on a new
object that has not yet been persisted. This behavior follows the
precedent set by update_columns.
2013-02-25 00:29:28 -05:00
Carlos Antonio da Silva
0bc301ef7d Improve AR changelog [ci skip] 2013-02-24 19:33:18 -03:00
Yves Senn
4ef75b63db don't apply invalid ordering when preloading hmt associations.
closes #8663.

When preloading a hmt association there two possible scenarios:

1.) preload with 2 queries: first hm association, then hmt with id IN ()
2.) preload with join: hmt association is loaded with a join on the hm association

The bug was happening in scenario 1.) with a normal order clause on the hmt association.
The ordering was also applied when loading the hm association, which resulted in the error.

This patch only applies the ordering the the hm-relation if we are performing a join (2).
Otherwise the order will only appear in the second query (1).
2013-02-24 20:51:39 +01:00
Xavier Noria
d65376fce4 Merge pull request #9400 from senny/remove_auto_explain_threshold_in_seconds
remove config.auto_explain_threshold_in_seconds
2013-02-24 11:16:35 -08:00
Yves Senn
d3688e02ca remove AR auto-explain (config.auto_explain_threshold_in_seconds)
We discussed that the auto explain feature is rarely used.
This PR removes only the automatic explain. You can still display
the explain output for any given relation using `ActiveRecord::Relation#explain`.

As a side-effect this should also fix the connection problem during
asset compilation (#9385). The auto explain initializer in the `ActiveRecord::Railtie`
forced a connection.
2013-02-24 20:01:04 +01:00
Rafael Mendonça França
e54acf1308 Do not type cast all the database url values.
We should only type cast when we need to use.

Related to 4b005fb371c2e7af80df7da63be94509b1db038c
2013-02-24 14:36:18 -03:00
Yves Senn
d98763a602 multiple actions for :on option with after_commit and after_rollback
Closes #988.
2013-02-21 15:02:52 +01:00
Xavier Noria
8fc6b9b4c8 ActiveRecord -> Active Record [ci skip] 2013-02-20 21:09:11 +01:00
Xavier Noria
aeb4592006 removes relation bang methods from the AR changelog [ci skip] 2013-02-20 20:37:00 +01:00
Xavier Noria
36113d0b79 puts a lambda around a default scope [ci skip] 2013-02-20 20:37:00 +01:00
Xavier Noria
c5115b06d7 database.yml -> config/database.yml [ci skip] 2013-02-20 20:36:59 +01:00
Yves Senn
de615b38b3 unify AR changelog entries [ci skip] 2013-02-20 18:29:44 +01:00
Yves Senn
39eef1a565 also rename indexes when a table or column is renamed
When a table or a column is renamed related indexes kept their name. This will lead to confusing names. This patch renames related indexes when a column or a table is renamed. Only indexes with names generated by rails will be renamed. Indexes with custom names will not be renamed.
2013-02-20 18:19:25 +01:00
Xavier Noria
20ed3e0f71 moves the new :nsec date format to the Active Support changelog [ci skip] 2013-02-20 18:08:55 +01:00
Yves Senn
cca43528d4 reserve index name chars for internal rails operations
Some adapter (SQLite3) need to perform renaming operations to support
the rails DDL. These rename prefixes operate with prefixes. When an
index name already uses up the full space provieded by
`index_name_length` these internal operations will fail. This patch
introduces `allowed_index_name_length` which respects the amount of
characters used for internal operations. It will always be <=
`index_name_length` and every adapter can define how many characters
need to be reserved.
2013-02-20 17:50:10 +01:00
Rafael Mendonça França
7bae72c69f Merge pull request #9332 from wangjohn/adding_documentation_to_error_raising_in_query_methods
Tests to make sure empty arguments in WhereChain raise errors
2013-02-20 08:38:50 -08:00
wangjohn
d49f862b9a Added comments about the check_empty_arguments method which is called
for query methods in a where_clause. Also, modified the CHANGELOG entry
because it had false information and added tests.
2013-02-20 10:53:25 -05:00
wangjohn
67d8bb963d Reduced memory leak problem in transactions by lazily updating AR objects with new transaction state. If AR object has a callback, the callback will be performed immediately (non-lazily) so the transaction still has to keep records with callbacks. 2013-02-20 10:51:28 -05:00
Yves Senn
0a6b61a5f5 descriptive error message when AR adapter was not found. Closes #7313. 2013-02-20 10:54:58 +01:00
Xavier Noria
276918098a s/revertible/reversible/ 2013-02-19 18:12:54 +01:00
Xavier Noria
ff259b5249 moves a CHANGELOG entry from AR to AP 2013-02-19 17:32:32 +01:00
Xavier Noria
2abf217d54 small tweaks in the AR CHANGELOG 2013-02-19 17:32:32 +01:00
John J Wang
4033c50000 Raise an error when query methods have blank arguments, and when blank arguments are meaningless. 2013-02-19 11:15:15 -05:00
Andrew White
bd328f4342 Add CHANGELOG entry for 41ff6a10216f48f43605a1f9cd6094765cab750f
[ci skip]
2013-02-17 16:48:01 +00:00
Yves Senn
4a4ff50459 don't cache invalid subsets when preloading hmt associations.
closes #8423.
2013-02-14 17:46:07 +01:00
James Miller
caabed6c76 Don't call after_commit when creating through an association and save fails, fixes #5802 2013-02-13 12:27:06 -08:00
Sergey Nartimov
9dfef5a46b define Active Record Store accessors in a module
Allow store accessors to be overrided like other attribute methods,
e.g.:

    class User < ActiveRecord::Base
      store :settings, accessors: [ :color, :homepage ], coder: JSON

      def color
        super || 'red'
      end
    end
2013-02-11 17:00:55 +03:00
Guillermo Iguaran
9c5cc89ede Update activerecord/CHANGELOG.md 2013-02-09 12:23:32 -05:00
Guillermo Iguaran
29f3a036e7 Reverting e170014113 (Change behaviour with empty hash in where clause) 2013-02-08 16:53:49 -05:00
Guillermo Iguaran
fa987cb8ec Reverting 16f6f25 (Change behaviour with empty array in where clause) 2013-02-08 14:22:10 -05:00
Carlos Antonio da Silva
01e67316d8 Improve changelog entries in AR
Describe better the changes related to raising errors with empty
array/hash values in where clauses.

[ci skip]
2013-02-08 11:29:48 -02:00
robertomiranda
16f6f2592e Change behaviour with empty array in where clause 2013-02-08 06:37:30 -05:00
robertomiranda
e170014113 Change behaviour with empty hash in where clause 2013-02-08 01:04:32 -05:00
Dylan Smith
a712e08ebe active_record: Quote numeric values compared to string columns. 2013-02-07 04:59:33 -05:00
Carlos Antonio da Silva
7173919af9 Add changelog entry for #9203 about schema dumper with db extensions
[ci skip]
2013-02-06 22:42:29 -02:00
Rafael Mendonça França
adb8fea493 CHANGELOG review 2013-01-31 17:48:59 -02:00
Aaron Stone
4b005fb371 DATABASE_URL parsing should turn numeric strings into numeric types, and
the strings true and false into boolean types, in order to match how
YAML would parse the same values from database.yml and prevent
unexpected type errors in the database adapters.
2013-01-31 09:01:20 -08:00
Carlos Antonio da Silva
2cb576ef36 Refactor to use each_key, remove extra spaces 2013-01-28 23:25:03 -02:00
Jon Leighton
c8d889905d Prevent Relation#merge from collapsing wheres on the RHS
This caused a bug with the new associations implementation, because now
association conditions are represented as Arel nodes internally right up
to when the whole thing gets turned to SQL.

In Rails 3.2, association conditions get turned to raw SQL early on,
which prevents Relation#merge from interfering.

The current implementation was buggy when a default_scope existed on the
target model, since we would basically end up doing:

  default_scope.merge(association_scope)

If default_scope contained a where(foo: 'a') and association_scope
contained a where(foo: 'b').where(foo: 'c') then the merger would see
that the same column is representated on both sides of the merge and
collapse the wheres to all but the last: where(foo: 'c')

Now, the RHS of the merge is left alone.

Fixes #8990
2013-01-27 20:34:52 +00:00
Lilibeth De La Cruz
bc982cbcb3 Fix handling of dirty time zone aware attributes
Previously, when `time_zone_aware_attributes` were enabled, after
changing a datetime or timestamp attribute and then changing it back
to the original value, `changed_attributes` still tracked the
attribute as changed. This caused `[attribute]_changed?` and
`changed?` methods to return true incorrectly.

Example:

    in_time_zone 'Paris' do
      order = Order.new
      original_time = Time.local(2012, 10, 10)
      order.shipped_at = original_time
      order.save
      order.changed? # => false

      # changing value
      order.shipped_at = Time.local(2013, 1, 1)
      order.changed? # => true

      # reverting to original value
      order.shipped_at = original_time
      order.changed? # => false, used to return true
    end
2013-01-26 16:57:33 +00:00
Yves Senn
648def4060 #count in conjunction with #uniq performs distinct count.
closes #6865
2013-01-26 12:16:28 +01:00
bUg
af1ef85ad2 Add postgresql range types support 2013-01-23 13:37:14 +01:00
wangjohn
bbcebb6ab2 Added a changelog entry. 2013-01-20 14:02:12 -05:00
Jon Leighton
5937bd02de Undeprecate the :extend option
Suggested by @dhh.

It doesn't affect the generated SQL, so seems reasonable to continue to
allow it as an association option.
2013-01-18 12:30:47 +00:00
Yves Senn
ec07735641 CollectionAssociation#empty? respects newly builded records 2013-01-13 00:20:38 +01:00
Prem Sichanugrist
ec53106068 Add CHANGELOG entry for YAML parsing removal
This is for commit e80546cdec56a9c3fcaf6217cba08a02d789d2bc.
2013-01-08 16:59:55 -05:00
Tristan Harward
807e176aba Fix error when assigning NaN to an integer column
Also covers any non-castable case by returning nil, which
is in-line with the intention of the former implementation,
but covers the odd cases which respond to to_i but raise
an error when it's called, such as NaN, Infinity and -Infinity.

Fixes #8757
2013-01-06 16:09:40 -05:00
Carlos Antonio da Silva
149b86d97b Move changelog entry in railties to the top
Other minor changelog improvements [ci skip]
2013-01-04 22:49:14 -02:00
Rob Worley
e209107a51 Support for PostgreSQL's ltree data type. 2013-01-04 21:22:34 +00:00
Jason Stirk
8d98c83bbc Fix undefined method `to_i' introduced since 3.2.8
This commit fixes a bug introduced in 96a13fc7 which breaks behaviour of
integer fields.

In 3.2.8, setting the value of an integer field to a non-integer (eg.
Array, Hash, etc.) would default to 1 (true) :

    # 3.2.8
    p = Post.new
    p.category_id = [ 1, 2 ]
    p.category_id # => 1
    p.category_id = { 3 => 4 }
    p.category_id # => 1

In 3.2.9 and above, this will raise a NoMethodError :

    # 3.2.9
    p = Post.new
    p.category_id = [ 1, 2 ]

    NoMethodError: undefined method `to_i' for [1, 2]:Array

Whilst at first blush this appear to be sensible, it combines in bad
ways with scoping.

For example, it is common to use scopes to control access to data :

    @collection = Posts.where(:category_id => [ 1, 2 ])
    @new_post = @collection.new

In 3.2.8, this would work as expected, creating a new Post object
(albeit with @new_post.category_id = 1). However, in 3.2.9 this will
cause the NoMethodError to be raised as above.

It is difficult to avoid triggering this error without descoping before
calling .new, breaking any apps running on 3.2.8 that rely on this
behaviour.

This patch deviates from 3.2.8 in that it does not retain the somewhat
spurious behaviour of setting the attribute to 1. Instead, it explicitly
sets these invalid values to nil :

    p = Post.new
    p.category_id = [ 1, 2 ]
    p.category_id # => nil

This also fixes the situation where a scope using an array will
"pollute" any newly instantiated records.

    @new_post = @collection.new
    @new_post.category_id # => nil

Finally, 3.2.8 exhibited a behaviour where setting an object to an
integer field caused it to be coerced to "1". This has not been
retained, as it is spurious and surprising in the same way that setting
Arrays and Heshes was :

    c = Category.find(6)
    p = Post.new

    # 3.2.8
    p.category_id = c
    p.category_id # => 1

    # This patch
    p.category_id = c
    p.category_id # => nil

This commit includes explicit test cases that expose the original issue
with calling new on a scope that uses an Array. As this is a common
situation, an explicit test case is the best way to prevent regressions
in the future.

It also updates and separates existing tests to be explicit about the
situation that is being tested (eg. AR objects vs. other objects vs.
non-integers)
2013-01-04 10:34:08 +11:00
Guillermo Iguaran
cb2bd4aa61 Merge pull request #8705 from amparo-luna/change_update_attributes_to_update
Rename update_attributes method to update
2013-01-03 09:09:59 -08:00
Amparo Luna + Guillermo Iguaran
d826f14443 Add Changelog entry regarding update_attibutes being renamed to update 2013-01-03 12:04:25 -05:00
Carlos Antonio da Silva
9f200fb296 Changelog improvements [ci skip] 2013-01-02 21:11:11 -02:00
Rafael Mendonça França
5d80da812b Fix CHANGELOG style [ci skip] 2012-12-26 12:19:09 -03:00
Pascal Friederich
5a3dc8092d validate :on option on after_commit and after_rollback callbacks 2012-12-26 11:37:37 +01:00
Marc-Andre Lafortune
a81845f268 Update Migration and 4.0 Release Guides, Changelogs [#8267] 2012-12-21 13:56:16 -05:00
Rafael Mendonça França
f2e0925ab7 Serialized attribute can be serialized in an integer column
Fix #8575
2012-12-21 14:41:13 -03:00
Carlos Antonio da Silva
42b555dcf3 Fix Action Mailer changelog indenation to match other changelogs
Also some minor improvements to other changelogs. [ci skip]
2012-12-19 23:09:04 -02:00
Yves Senn
d01f913f8d Keep index names when using with sqlite3 2012-12-19 09:07:09 +01:00
Gary S. Weaver
9bb27f7ffe #5523 Add ability for postgresql adapter to disable user triggers in disable_referential_integrity. 2012-12-18 10:46:16 -05:00
Pedro Padron
ba98dad113 Added support for validates_uniqueness_of in PostgreSQL array columns. Fixes: #8075. 2012-12-18 08:35:03 -02:00
Alexey
9a4a095ed7 AR supporting new intrange data type on PostgreSQL >= 9.2 2012-12-16 13:11:10 +02:00
Carlos Antonio da Silva
df9f9b7f3e Convert changelogs to 1.9 hash style and fix some formatting [ci skip] 2012-12-14 20:25:22 -02:00
Matthew M. Boedicker
8f59ffce67 Do not log the binding values for binary columns.
They tend to be large and not very useful in the log.
2012-12-14 12:19:17 -08:00
Matthew Robertson
a765c8426f Fix for has_many_through counter_cache bug
This commit fixes reported issue #7630 in which counter
caches were not being updated properly when replacing
has_many_through relationships
2012-12-14 10:03:05 -08:00
Yves Senn
349d460600 recognize migrations, in folders containing numbers and 'rb'.
Closes #8492
2012-12-13 09:08:32 +01:00
Carlos Antonio da Silva
2d3b1dabc9 Remove changelog entry from #8441 [ci skip]
This issue only happens on master due to internal AR refactorings, so
there is no need for a changelog entry.

The test was backported to 3-2-stable to ensure there won't be any regressions.
2012-12-11 08:33:58 -02:00
Rafael Mendonça França
d668544785 Add CHANGELOG entry for #6376.
Conflicts:
	activerecord/CHANGELOG.md
2012-12-10 17:48:31 -03:00
Rafael Mendonça França
9cce1ea2fd Allow users to choose the timestamp format in the cache key
This can be done using the class attribute cache_timestamp_format

Conflicts:
	railties/guides/source/configuring.textile
2012-12-10 17:48:26 -03:00
itzki
7efcc5d72f Fix decorating columns for serialized attributes 2012-12-10 14:44:24 +09:00
Jeremy Kemper
cff747d304 Move to the schema-migrations-metadata branch.
Pending work on graceful app upgrades.

Revert "Merge pull request #8439 from joshsusser/fixes"

This reverts commit ce8ac39338f86388e70356b3a470b3ea443802ae, reversing
changes made to b0e7b6f67c984d4b1502e801781ed75fad681633.

Revert "Merge pull request #8431 from joshsusser/schemadump"

This reverts commit 036d3e1c2b65c4b8cbd23de2e20ad67b9b756182, reversing
changes made to 0c692f4d121792117b6a71e5ed590a31c3b9d12e.

Revert "Merge branch 'joshsusser-master' into merge"

This reverts commit 0c692f4d121792117b6a71e5ed590a31c3b9d12e, reversing
changes made to 2e299fca715b083a60222a85e48f9d3b8dd8ce93.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
	activerecord/test/cases/schema_dumper_test.rb
2012-12-09 15:57:34 -07:00
Aaron Stone
97d06e8c4b Session variables for mysql, mysql2, and postgresql adapters can be set
in the new 'variables:' hash in each database config section in database.yml.
The key-value pairs of this hash will be sent in a 'SET key = value, ...'
query on new database connections.

The configure_connection methods from mysql and mysql2 into are
consolidated into the abstract_mysql base class.
2012-12-08 10:11:10 -08:00
Caike Souza
ad59260f70 Update AR Changelog with correct example using includes
These queries don't seem to work without the includes clause. [ci skip]
2012-12-08 08:16:13 -02:00
Carlos Antonio da Silva
8d02afeaee Rollback where.like and where.not_like
The real win with these chain methods is where.not, that takes care of
different scenarios in a graceful way, for instance when the given value
is nil.

    where("author.id != ?", author_to_ignore.id)
    where.not("author.id", author_to_ignore.id)

Both where.like and where.not_like compared to the SQL versions doesn't
seem to give us that much:

    Post.where("title LIKE 'ruby on%'")
    Post.where.like(title: 'ruby on%'")
    Post.where("title NOT LIKE 'ruby on%'")
    Post.where.not_like(title: 'ruby on%'")

Thus Rails is adding where.not, but not where.like/not_like and others.
2012-12-07 16:52:55 -02:00
Carlos Antonio da Silva
23b9cc8423 Merge pull request #8332 from amatsuda/ar_where_chain
Relation.where with no args can be chained with not, like, and not_like

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/query_methods.rb
2012-12-07 00:46:06 -02:00
Carlos Antonio da Silva
0e67f793cd Unscope update_column(s) query to ignore default scope
When applying default_scope to a class with a where clause, using
update_column(s) could generate a query that would not properly update
the record due to the where clause from the default_scope being applied
to the update query.

    class User < ActiveRecord::Base
      default_scope where(active: true)
    end

    user = User.first
    user.active = false
    user.save!

    user.update_column(:active, true) # => false

In this situation we want to skip the default_scope clause and just
update the record based on the primary key. With this change:

    user.update_column(:active, true) # => true

Fixes #8436.
2012-12-06 23:17:56 -02:00
Carlos Antonio da Silva
1eaf3db804 Improve AR changelog 2012-12-06 22:24:00 -02:00
Steve Klabnik
b27f27d38d CHANGELOG for 78d5d6f.
It was pointed out by @giner that the CHANGELOG entry for 78d5d6f868
wasn't included. Here it is.
2012-12-06 10:43:33 -08:00
Aaron Patterson
0c692f4d12 Merge branch 'joshsusser-master' into merge
* joshsusser-master:
  style cleanup
  Add migration history to schema.rb dump
  Add metadata to schema_migrations

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/schema.rb
2012-12-05 12:13:49 -08:00
claudiob
019df98875 Replace comments' non-breaking spaces with spaces
Sometimes, on Mac OS X, programmers accidentally press Option+Space
rather than just Space and don’t see the difference. The problem is
that Option+Space writes a non-breaking space (0XA0) rather than a
normal space (0x20).

This commit removes all the non-breaking spaces inadvertently
introduced in the comments of the code.
2012-12-04 22:11:54 -08:00
kennyj
634d28e6cf Fix #8414. Performance problem with postgresql adapter primary_key function. 2012-12-05 02:19:03 +09:00
Yves Senn
db51704bd9 Do not instantiate intermediate AR objects when eager loading.
Closes #3313
2012-12-04 11:52:08 +01:00
Josh Susser
f02d2185eb Add migration history to schema.rb dump 2012-12-02 21:16:32 -08:00
Josh Susser
0a5afa229d Add metadata to schema_migrations
migrated_at: timestamp when migration run
fingerprint: md5 hash of migration source
name: filename without version or extension
2012-12-01 19:41:09 -08:00
Akira Matsuda
de75af7acc Relation.where with no args can be chained with not, like, and not_like
examples:

  Model.where.not field: nil
  #=> "SELECT * FROM models WHERE field IS NOT NULL

  Model.where.like name: 'Jeremy%'
  #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%'

this feature was originally suggested by Jeremy Kemper https://github.com/rails/rails/pull/5950#issuecomment-5591330

Closes #5950
2012-11-30 01:18:18 +09:00
Jason Rush
89b5b31cc4 Added STI support to init and building associations
Allows you to do BaseClass.new(:type => "SubClass") as well as
parent.children.build(:type => "SubClass") or parent.build_child
to initialize an STI subclass. Ensures that the class name is a
valid class and that it is in the ancestors of the super class
that the association is expecting.
2012-11-29 05:50:34 +00:00
Rafael Mendonça França
ec17f0d352 Add CHANGELOG entries for the observes extraction
[ci skip]
2012-11-29 01:08:36 -02:00
Steve Klabnik
22df38ca33 Ensure that associations have a symbol argument.
Fixes #7418.
2012-11-28 13:39:42 -08:00
Rafael Mendonça França
f3aaf6d7d1 Use assert_nil instead of assert_equal 2012-11-28 12:02:11 -02:00
Alisdair McDiarmid
fc4e387d7a Don't call will_change! for datetime nil->"".
Setting a nil datetime attribute to a blank string should not cause the
attribute to be dirty.

Fix #8310
2012-11-25 11:24:27 +00:00
Carlos Antonio da Silva
d23c761f5a Fix changelog entry about fast_string_to_time fix
Introduced in 53ca22f2e11cd3050d75385bc31b6bb5055a2738.
Thanks @rochefort. [ci skip]
2012-11-24 09:30:18 -02:00
Rafael Mendonça França
b313bcba07 Merge pull request #8291 from senny/8265_build_with_polymorphic_association
prevent mass assignment of polymorphic type when using `build`

Conflicts:
	activerecord/CHANGELOG.md
2012-11-22 11:52:01 -02:00
Yves Senn
053bfa2304 prevent mass assignment of polymorphic type when using build
Closes #8265
2012-11-22 08:58:00 +01:00
Carlos Antonio da Silva
ad9983f625 Deprecate Relation#sum with a block.
To perform a sum calculation over the array of elements, use to_a.sum(&block).

Please check the discussion in f9cb645dfcb5cc89f59d2f8b58a019486c828c73
for more context.
2012-11-21 22:21:01 -02:00
Carlos Antonio da Silva
76a6bfd6c8 Revert "Yield only one argument instead of splatting."
This reverts commit f9cb645dfcb5cc89f59d2f8b58a019486c828c73.

Conflicts:
	activerecord/CHANGELOG.md

Revert "Allow blocks for count with ActiveRecord::Relation. Document and test that sum allows blocks"

This reverts commit 9cc2bf69ce296b7351dc612a8366193390a305f3.

Conflicts:
	activerecord/lib/active_record/relation/calculations.rb
2012-11-21 22:18:51 -02:00
Bogdan Gusiev
fa73cf7275 Fix postgresql adapter to handle bc timestamps correctly 2012-11-21 16:11:14 +02:00
Victor Costan
a8a60e9240 Postgresql doesn't accepts limits on text columns. 2012-11-20 10:59:40 -05:00
Rafael Mendonça França
0cc9c1255d Merge pull request #3023 from Tho85/preserve_sti_type
AR::Base.becomes should not change the STI type

Conflicts:
	activerecord/CHANGELOG.md
2012-11-18 23:25:13 -02:00
Jarek Radosz
1a782b2b63 Add rename_index to change_table. 2012-11-19 02:10:28 +01:00
Thomas Hollstegge
70fa756ddb AR::Base.becomes should not change the STI type
If you want to change the STI type too, use AR::Base.becomes! instead
2012-11-17 16:45:23 +01:00
Carlos Antonio da Silva
7411970398 Fix some AR changelog entries [ci skip] 2012-11-16 21:37:53 -02:00
Yves Senn
663d9ef670 #pluck can be used on a relation with select clause.
Closes #7551
2012-11-12 16:27:44 +01:00
Yves Senn
d5ac2801d9 cleanup, removed whitespace from CHANGELOG 2012-11-12 16:06:58 +01:00
Bogdan Gusiev
3cb0f3feed Do not create useless database transaction when building has_one association. 2012-11-10 15:30:20 +02:00
Yves Senn
710e371081 :counter_cache option for to support custom named counter caches. Closes #7993 2012-11-04 17:09:25 +01:00
Rafael Mendonça França
7042fe2f84 Deprecate passing a string as third argument of add_index
This was there due historical reasons since
7dc45818dc43c163700efc9896a0f3feafa31138 to give the user the
possibility to create unique indexes passing "UNIQUE" as the third
argument
2012-11-02 20:03:26 -02:00
Rafael Mendonça França
8fc52706c3 Raise an ArgumentError when passing an invalid option to add_index
Closes #8104
2012-11-02 19:00:58 -02:00
Alexis Bernard
a58cafeb3a Fix find_in_batches against string IDs when start option is not specified. 2012-10-31 15:39:24 +01:00
Nikita Afanasenko
e7e59a75aa Fix attributes_before_type_cast for serialised attributes.
Public method `attributes_before_type_cast` used to return internal AR structure (ActiveRecord::AttributeMethods::Serialization::Attribute), patch fixes this. Now behaves like `read_attribute_before_type_cast` and returns unserialised values.
2012-10-31 13:16:44 +04:00
kennyj
a7c3c90250 Fix #6951. Use query cache/uncache, when using not only database.yml but also DATABASE_URL. 2012-10-31 10:04:10 +09:00
Juanjo Bazán
300d080ada ActiveRecord::Relation#none! method. 2012-10-28 22:18:45 +01:00
Henrik N
1849665f73 Enable update_column(s) for the primary key attribute.
Didn't work before because it updated the model-in-memory first, so the DB query couldn't find the record.
2012-10-28 21:28:54 +01:00
Yves Senn
e4790a2c5b raise ArgumentError when redefining the primary key column. Closes #6378 2012-10-28 20:40:37 +01:00
Francesco Rodriguez
10f6f90d9d AR::AttributeMethods#[] raises AM::AttributeMissingError for missing attributes.
This fixes the following behaviour:

    class Person < ActiveRecord::Base
      belongs_to :company
    end

    # Before:
    person = Person.select('id').first
    person[:name]       # => nil
    person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
    person[:company_id] # => nil
    person.company      # => nil

    # After:
    person = Person.select('id').first
    person[:name]       # => ActiveModel::MissingAttributeError: missing_attribute: name
    person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
    person[:company_id] # => ActiveModel::MissingAttributeError: missing_attribute: company_id
    person.company      # => ActiveModel::MissingAttributeError: missing_attribute: company_id

Fixes #5433.
2012-10-28 14:18:31 -05:00
Victor Costan
5d30e44390 Use the MySQL varbinary type when appropriate in migrations. 2012-10-27 13:41:27 -04:00
Shawn Veader
f96b410bc7 Decode attributes pulled from URI.parse
The RFC indicates that username and passwords may be encoded.
http://tools.ietf.org/html/rfc2396#section-3.2.2

Found this trying to use the mysql://username:password@host:port/db and having special characters in the password which needed to be URI encoded.
2012-10-26 15:43:07 -04:00
Jon Leighton
9e4c41c903 Remove ActiveRecord::Model
In the end I think the pain of implementing this seamlessly was not
worth the gain provided.

The intention was that it would allow plain ruby objects that might not
live in your main application to be subclassed and have persistence
mixed in. But I've decided that the benefit of doing that is not worth
the amount of complexity that the implementation introduced.
2012-10-26 15:51:02 +01:00
Uģis Ozols
1a6b082808 Fix incorrect markdown by removing extra space. 2012-10-24 16:15:48 +03:00
Jeremy Kemper
4ed156336f Revert "Get rid of the ActiveRecord::Model::DeprecationProxy thing."
This reverts commit 83846838252397b3781eed165ca301e05db39293.
2012-10-20 11:35:16 -07:00
Jon Leighton
8384683825 Get rid of the ActiveRecord::Model::DeprecationProxy thing.
I think it's going to be too much pain to try to transition the
:active_record load hook from executing against Base to executing
against Model.

For example, after Model is included in Base, and modules included in
Model will no longer get added to the ancestors of Base.

So plugins which wish to be compatible with both Model and Base should
use the :active_record_model load hook which executes *before* Base gets
loaded.

In general, ActiveRecord::Model is an advanced feature at the moment and
probably most people will continue to inherit from ActiveRecord::Base
for the time being.
2012-10-19 18:12:54 +01:00
Jon Leighton
7efb1feaa3 Rename the partial_updates config to partial_writes
This reflects the fact that it now impact inserts as well as updates.
2012-10-19 16:45:41 +01:00
Jon Leighton
0096f53b25 nodoc the first_or_create methods and document alternatives 2012-10-19 15:56:18 +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
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
Carlos Antonio da Silva
d207a33e00 Move changelog entry from #7439 to the top [ci skip] 2012-10-15 19:19:34 -03:00
Tim Macfarlane
51d6e21c96 ActiveRecord: sum expression returns string '0' for no records, fixed 2012-10-15 22:31:05 +01:00
Arturo Pie
54b3f41741 #7914 Add change of previous commit to CHANGELOG.md 2012-10-13 22:18:50 -04:00
Tima Maslyuchenko
633ea6a826 learn ActiveRecord::QueryMethods#order work with hash arguments 2012-10-12 17:57:24 +03:00
Alexey Muranov
bf4d6a2353 Add CHANGELOG entry for "Fixtures" -> "FixtureSet" 2012-10-07 20:43:18 +02:00
Yves Senn
16e4a53b78 PostgreSQL, quote table names when fetching the primary key. Closes #5920 2012-10-05 08:54:38 +02:00
Rafael Mendonça França
d7d228402e Fix CHANGELOG entry [ci skip] 2012-10-04 10:23:27 -03: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
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
Rafael Mendonça França
77fbe1c019 Add missing CHANGELOG entry removed by mistake at 7f3b475 [ci skip] 2012-09-28 14:12:17 -03: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
Sebastian Martinez
bca66a3547 Add #update_columns entry to AR Changelog. 2012-09-28 13:58:07 -03:00
John Foley
f31ea4df3a Add CHANGELOG entry and update the guide 2012-09-23 12:57:19 -06: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
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
Francesco Rodriguez
f4a9d7db1c remove unnecessary entry and make minor edits to AR/CHANGELOG [ci skip] 2012-09-20 13:48:32 -05:00
Francesco Rodriguez
a30b8d38b4 rename AR::Model::Tag to AR::Tag - fixes #7714 2012-09-20 12:43:12 -05:00
Guillermo Iguaran
1f4c89fbde Update changelogs to add entries about strong_parameters integration 2012-09-19 17:50:39 -05:00
Rafael Mendonça França
69e0e3f91b Improve the CHANGELOG entry for #6971 2012-09-19 12:09:36 -03:00
Damien Mathieu
30a576fa14 fix querying with an empty hash
Closes #6960
2012-09-19 15:57:22 +02:00