Commit Graph

882 Commits

Author SHA1 Message Date
Zachary Scott
1d7b3fa84c Future port c8ddb61 2014-05-23 14:45:21 -07:00
Godfrey Chan
2d73f5ae2f Fixed serialization for records with an attribute named format.
* * *

This bug can be triggered when serializing record R (the instance) of type C
(the class), provided that the following conditions are met:

1. The name of one or more columns/attributes on C/R matches an existing private
   method on C (e.g. those defined by `Kernel`, such as `format`).

2. The attribute methods have not yet been generated on C.

In this case, the matching private methods will be called by the serialization
code (with no arguments) and their return values will be serialized instead. If
the method requires one or more arguments, it will result in an `ArgumentError`.

This regression is introduced in d1316bb.

* * *

Attribute methods (e.g. `#name` and `#format`, assuming the class has columns
named `name` and `format` in its database table) are lazily defined. Instead of
defining them when a the class is defined (e.g. in the `inherited` hook on
`ActiveRecord::Base`), this operation is deferred until they are first accessed.

The reason behind this is that is defining those methods requires knowing what
columns are defined on the database table, which usually requires a round-trip
to the database. Deferring their definition until the last-possible moment helps
reducing unnessary work, especially in development mode where classes are
redefined and throw away between requests.

Typically, when an attribute is first accessed (e.g. `a_book.format`), it will
fire the `method_missing` hook on the class, which triggers the definition of
the attribute methods. This even works for methods like `format`, because
calling a private method with an explicit receiver will also trigger that hook.

Unfortunately, `read_attribute_for_serialization` is simply an alias to `send`,
which does not respect method visibility. As a result, when serializing a record
with those conflicting attributes, the `method_missing` is not fired, and as a
result the attribute methods are not defined one would expected.

Before d1316bb, this is negated by the fact that calling the `run_callbacks`
method will also trigger a call to `respond_to?`, which is another trigger point
for the class to define its attribute methods. Therefore, when Active Record
tries to run the `after_find` callbacks, it will also define all the attribute
methods thus masking the problem.

* * *

The proper fix for this problem is probably to restrict `read_attribute_for_serialization`
to call public methods only (i.e. alias `read_attribute_for_serialization` to
`public_send` instead of `send`). This however would be quite risky to change
in a patch release and would probably require a full deprecation cycle.

Another approach would be to override `read_attribute_for_serialization` inside
Active Record to force the definition of attribute methods:

   def read_attribute_for_serialization(attribute)
     self.class.define_attribute_methods
     send(attribute)
   end

Unfortunately, this is quite likely going to cause a performance degradation.

This patch therefore restores the behaviour from the 4-0-stable branch by
explicitly forcing the class to define its attribute methods in a similar spot
(when records are initialized). This should not cause any extra roundtrips to
the database because the `@columns` should already be cached on the class.

Fixes #15188.
2014-05-22 14:35:04 -07:00
Matthew Draper
110d3d0c0b Merge pull request #14803 from kuldeepaggarwal/null_relation_sum_fix
Fixed a problem where `sum` used with a `group` was not returning a Hash.
2014-05-22 14:59:23 +09:30
Rafael Mendonça França
94e7dbce8a Merge pull request #14613 from Sirupsen/fix-serialize-update-column
Fix serialized field returning serialized data after update_column

Conflicts:
	activerecord/CHANGELOG.md
2014-05-21 12:38:11 -03:00
Rafael Mendonça França
6931bed1b4 Revert "Merge pull request #14544 from jefflai2/named_scope_sti"
This reverts commit 9a1abedcdeecd9464668695d4f9c1d55a2fd9332, reversing
changes made to c72d6c91a7c0c2dc81cc857a1d6db496e84e0065.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/models/comment.rb

This change break integration with activerecord-deprecated_finders so
I'm reverting until we find a way to make it work with this gem.
2014-05-21 12:15:57 -03:00
Lauro Caetano
4debc86bd3 Fix polymorphic eager load with foreign_key as String.
The foreign_key could be `String` and just doing `owners_map[owner_key]`
could return `nil`.
To prevent this bug, we should `to_s` both keys if their types are
different.

Fixes #14734.
2014-05-20 23:26:51 -03:00
Rafael Mendonça França
9a1abedcde Merge pull request #14544 from jefflai2/named_scope_sti
Fixes Issue #13466.

Conflicts:
	activerecord/CHANGELOG.md
2014-05-20 21:35:58 -03:00
Rafael Mendonça França
437f464b39 Merge pull request #14979 from brocktimus/master
Making belongs_to: touch behaviour be consistent with save updating updated_at
2014-05-20 21:03:56 -03:00
Rafael Mendonça França
26931eb320 Revert "Revert "Merge pull request #8313 from alan/only_save_changed_has_one_objects""
This reverts commit e94e6c27af495a2460c811bb506459f1428dec6b.

Conflicts:
	activerecord/CHANGELOG.md

The original commit was reverted only to be safe since #14407 were reported.
We don't have any proof we added a regression with the original commit
so reverting it now will give us more problem.

Closes #14407
2014-05-20 20:42:46 -03:00
Brock Trappitt
713fc39d93 Prevented belongs_to: touch propagating up if there are no changes being saved 2014-05-21 07:34:53 +08:00
Eric Chahin
1f4daf15f8 Fixed the inferred table name for HABTM within a schema
Fixes #14824.
2014-05-20 23:13:28 +09:30
Sean Griffin
d0f8c46e19 Remove :timestamp column type
The `:timestamp` type for columns is unused. All database adapters treat
them as the same database type. All code in `ActiveRecord` which changes
its behavior based on the column's type acts the same in both cases.
However, when the type is passed to code that checks for the `:datetime`
type, but not `:timestamp` (such as XML serialization), the result is
unexpected behavior.

Existing schema definitions will continue to work, and the `timestamp`
type is transparently aliased to `datetime`.
2014-05-19 11:32:13 -07:00
Rafael Mendonça França
889f61efff Merge pull request #10798 from jcxplorer/fix-enable_extension-with-table_name_prefix
Fix migrations that use enable_extension with table_name_prefix/suffix

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/migration.rb
2014-05-19 13:32:15 -03:00
Marc Schütz
9a0d35e820 Make :index in migrations work with all column types 2014-05-18 11:26:41 +02:00
Rafael Mendonça França
067131524b Merge pull request #14428 from jnormore/reset_counters_alias
Updates reset_counters to allow counter name in params
2014-05-17 16:21:41 -03:00
Leandro Facchinetti
7e35675059 Fix typo in CHANGELOG entry for #15071 2014-05-17 01:59:55 -03:00
Jason Normore
fec7bfe8d1 Updates reset_counters to allow counter name in params
Add support for counter name to be passed as parameter
on `CounterCache::ClassMethods#reset_counters`. This is
to be consistent with the other methods in the module
that all accept counter name.
2014-05-16 22:27:44 -04:00
Rafael Mendonça França
a9ebfc0f6a Add CHANGELOG entry for #15071 [ci skip] 2014-05-16 21:01:47 -03:00
Yves Senn
8109dc8067 formatting pass through CHANGELOGS. [ci skip] 2014-05-16 09:03:26 +02:00
Aaron Nelson
51de8cee82 Changed extract_limit in class Column to return correct mysql float and double limits 2014-05-16 01:39:24 +09:30
Rafael Mendonça França
f85371f69d Merge pull request #14871 from kassio/kb-fixes-namespaced-habtm
Fix how to compute class name on habtm namespaced.

Conflicts:
	activerecord/CHANGELOG.md
2014-05-14 21:35:52 -03:00
Rafael Mendonça França
ec6bb33209 Merge pull request #15078 from nbudin/fix_merger_filter_binds_comparison_master
Make filter_binds filter out symbols that are equal to strings

Conflicts:
	activerecord/CHANGELOG.md
2014-05-14 20:29:30 -03:00
Nat Budin
1d316ac1fd Make filter_binds filter out symbols that are equal to strings
ActiveRecord::Relation::Merger's filter_binds method does not filter out bind
variables when one of the attribute nodes has a string name, but the other has
a symbol name, even when those names are actually equal.

This can result in there being more bind variables than placeholders in the
generated SQL.  This is particularly an issue for PostgreSQL, where this is
treated as an error.

This patch changes the filter_binds method to make it convert both attribute
names to strings before comparing.
2014-05-14 16:18:42 -07:00
Kuldeep Aggarwal
6d36c1dd05 Fixed a problem where sum, size, average, minimum and maximum used
with a grouping was not returning a Hash.
2014-05-15 00:49:29 +05:30
Jessica Yao
6de710d664 Fix inheritance of stored_attributes (fixes #14672)
[Brad Bennett, Jessica Yao, & Lakshmi Parthasarathy]
2014-05-14 22:32:13 +09:30
Kassio Borges
8f6e5986ac Fix how to compute class name on habtm namespaced.
Thank's for @laurocaetano for the help with tests. 😃

Fixes #14709
2014-05-13 11:26:46 -03:00
Yves Senn
096be96db8 pg, change_column_default accepts []. Closes #11586. 2014-05-12 17:28:45 +02:00
Yves Senn
711af75234 pg, map char and name types as string. [dark-panda & Yves Senn]
Closes #10802.
2014-05-12 16:39:24 +02:00
Innokenty Mihailov
aac40bcc07 pg, fix Infinity and NaN values conversion.
Before this patch `Infinity`, `-Infinity` and `Nan` were read as `0`.
2014-05-12 15:46:48 +02:00
Patrick Robertson
c0a1245341 Handle other pk types in PostgreSQL gracefully.
In #10410 it was noted that you can no longer create PK's with the
type of bigserial in PostgreSQL in 4.0.0.rc1. This is mostly
because the newer adapter is checking for column type with the
id column instead of just letting it pass through like it did
before.

Side effects:
You may just create a PK column of a type that you really don't
want to be your PK. As far as I can tell this was allowed in 3.2.X
and perhaps an exception should be raised if you try and do
something extremely dumb.
2014-05-12 10:55:52 +02:00
Yves Senn
ed56e596a0 deprecate, join, preload, eager load of instance dependent associations.
Closes #15024.

These operations happen before instances are created.
The current behavior is misleading and can result in broken behavior.
2014-05-10 13:51:31 +02:00
Arthur Neves
198d9e3dfb Reverts "Fix bugs with changed attributes tracking when transaction gets rollback"
We are reverting these commits, because there are other caveats related
to dirty attributes not being restored when a transaction is rollback.
For instance, nested transactions cannot proper restore the dirty
attributes state after a rollback.

At the moment, the dirty attributes are scoped by the transaction.
When we call `.save` on a record, the dirty attributes will be reset even
if the transaction gets rollback.

[related #13166]
[related #15018]
[related #15016]
[closes #15019]

This reverts commits
* bab48f0a3d53a08bc23ea0887219e8deb963c3d9
  * b0fa7cf3ff8432cc2eef3682b34763b7f8c93cc8.
* 73fb39b6faa9de593ae75ad4e3b8e065ea0e53af
  * 37c238927fbed059de3f26a90d8923fb377568a5.
  * 8d8d4f1560264cd1c74364d67fa0501f6dd2c4fa

Revert "Merge pull request #13166 from bogdan/transaction-magic"
2014-05-09 13:42:04 -04:00
Fred Wu
f045663dfc Fixed HABTM's CollectionAssociation size
HABTM should fall back to using the normal CollectionAssociation's size calculation if the collection is not cached or loaded.

This addresses both #14913 and #14914 for master.
2014-05-08 16:01:57 +10:00
Rafael Mendonça França
f274fbd240 Add CHANGELOG entry for #14989
Closes #14989
2014-05-07 18:36:06 -03:00
Arthur Neves
37c238927f Keep track of dirty attrs after after rollback.
[related #13166]
2014-05-07 17:39:24 -03:00
Jenner LaFave
5de7309b7b Add support for module-level table_suffix in models
This makes table_name_suffix work the same as table_name_prefix when
using namespaced models. Pretty much the same as 67d1cec.
2014-05-05 12:41:26 -07:00
Rafael Mendonça França
4efb0f3730 Improve CHANGELOG entry [ci skip] 2014-05-05 13:23:29 -03:00
Bogdan Gusiev
7e20bdc20d Put back Relation#join method as a delegate to Array
This is a regression 4.0 -> 4.1 fix.
In 4.1.0 Relation#join is delegated to Arel#SelectManager.
In 4.0 series it is delegated to Array#join

This patch puts back the behaviour of 4.0
2014-05-05 16:45:16 +03:00
Rafael Mendonça França
7922ccb261 Merge pull request #14946 from jcoleman/fix-null-binary-column-logging-exception
Fix exception when logging SQL w/ nil binary value.

Conflicts:
	activerecord/CHANGELOG.md
2014-05-04 18:20:51 -03:00
Matthew Draper
022c797926 Merge pull request #14924 from eric-chahin/issue_13854
Fixed custom validation context bug for child associations
2014-05-03 16:29:40 +09:30
James Coleman
12ff63b227 Fix exception when logging SQL w/ nil binary value. 2014-05-02 12:39:40 -04:00
Robin Dupret
5c87c95a71 Enhance a bit a few changelog entries [ci skip] 2014-05-02 11:56:03 +02:00
Rafael Mendonça França
e99e23d115 Merge pull request #14896 from jetthoughts/14895_overide_strict_by_sql_mode
Symbolize variables of mysql/mysql2 connection configuration

Conflicts:
	activerecord/CHANGELOG.md
2014-05-01 17:49:20 -03:00
Rafael Mendonça França
372e28ce22 Merge pull request #13640 from maginatics/fix_sqlite3_ensure_master
SQLite3: Always close statements.

Conflicts:
	activerecord/CHANGELOG.md
2014-05-01 14:10:04 -03:00
Eric Chahin
c6d685ba11 Fixed custom validation context bug where child
associations were not being saved.

Fixes #13854.

[Eric Chahin, Aaron Nelson, & Kevin Casey]
2014-05-01 03:27:24 -04:00
Hector Satre
c3439457e3 Give real privacy to class methods in AR::PredicateBuilder 2014-05-01 08:16:33 +02:00
Paul Nikitochkin
7e8b062823 Stringify variables names for mysql connections
For mysql2/mysql adapters, `sql_mode` variable name set in `database.yml`
as string, was ignored and `sql_mode` was set to use strict mode.

Fixes #14895
2014-04-30 22:56:27 +03:00
Kassio Borges
18fa87b866 Fix custom join_table name on habtm reflections
When used a custom join_table name on a habtm, rails was not saving it
on Reflections. This causes a problem when rails loads fixtures, because
it uses the reflections to set database with fixtures.
2014-04-25 17:34:13 -03:00
Yves Senn
1b7aa62b18 reset @arel when modifying a Relation in place.
/cc @tenderlove
2014-04-24 14:29:18 -05:00
Jefferson Lai
d080e8c201 PostgreSQL Timestamps always map to :datetime.
The PG Adapter should use `:datetime` consistently instead of mapping
mispellings to `:timestamp`.

See #14513
2014-04-24 10:16:13 -07:00