aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/builder
Commit message (Collapse)AuthorAgeFilesLines
* Introduce `reload_<association>` reader for singular associations.Yves Senn2016-11-221-1/+10
| | | | | | | | | | | | | | | | This patch brings back the functionality of passing true to the association proxy. The behavior was deprecated with #20888 and scheduled for removal in Rails 5.1. The deprecation mentioned that instead of `Article.category(true)` one should use `article#reload.category`. Unfortunately the alternative does not expose the same behavior as passing true to the reader did. Specifically reloading the parent record throws unsaved changes and other caches away. Passing true only affected the association. This is problematic and there is no easy workaround. I propose to bring back the old functionality by introducing this new reader method for singular associations.
* Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-011-14/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks We pretty frequently get bug reports that "dirty is broken inside of after callbacks". Intuitively they are correct. You'd expect `Model.after_save { puts changed? }; model.save` to do the same thing as `model.save; puts model.changed?`, but it does not. However, changing this goes much farther than just making the behavior more intuitive. There are a _ton_ of places inside of AR that can be drastically simplified with this change. Specifically, autosave associations, timestamps, touch, counter cache, and just about anything else in AR that works with callbacks have code to try to avoid "double save" bugs which we will be able to flat out remove with this change. We introduce two new sets of methods, both with names that are meant to be more explicit than dirty. The first set maintains the old behavior, and their names are meant to center that they are about changes that occurred during the save that just happened. They are equivalent to `previous_changes` when called outside of after callbacks, or once the deprecation cycle moves. The second set is the new behavior. Their names imply that they are talking about changes from the database representation. The fact that this is what we really care about became clear when looking at `BelongsTo.touch_record` when tests were failing. I'm unsure that this set of methods should be in the public API. Outside of after callbacks, they are equivalent to the existing methods on dirty. Dirty itself is not deprecated, nor are the methods inside of it. They will only emit the warning when called inside of after callbacks. The scope of this breakage is pretty large, but the migration path is simple. Given how much this can improve our codebase, and considering that it makes our API more intuitive, I think it's worth doing.
* Fix HABTM associations join table resolver bug on constants and symbolsMehmet Emin İNAÇ2016-10-271-1/+1
| | | | | | | | | | | | | | Using Constant and symbol class_name option for associations are valid but raises exception on HABTM associations. There was a test case which tries to cover symbol class_name usage but doesn't cover correctly. Fixed both symbol usage and constant usage as well. These are all working as expected now; ``` has_and_belongs_to_many :foos, class_name: 'Foo' has_and_belongs_to_many :foos, class_name: :Foo has_and_belongs_to_many :foos, class_name: Foo ``` Closes #23767
* applies remaining conventions across the projectXavier Noria2016-08-061-1/+0
|
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-22/+22
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-062-2/+2
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Move the warning about composite primary key to `AttributeMethods::PrimaryKey`Ryuta Kamizono2016-07-021-2/+4
| | | | | | | | | | | Actually schema dumper/creation supports composite primary key (#21614). Therefore it should not show the warning about composite primary key in connection adapter. This change moves the warning to `AttributeMethods::PrimaryKey` and suppress the warning for habtm join table. Fixes #25388.
* Fix counter_cache double increment bugTom Kadwill2016-04-281-0/+2
|
* don't treat all associations with extensions as instance dependent.Yves Senn2016-03-031-1/+5
| | | | | | | | | | Closes #23934. This is a forward port of ac832a43b4d026dbad28fed196d2de69ec9928ac Previously the scope of all associations with extensions were wrapped in an instance dependent proc. This made it impossible to preload such associations.
* eliminate warnings about multiple primary keys on habtm join tablesAaron Patterson2016-02-191-0/+3
| | | | | | | | habtm join tables commonly have two id columns and it's OK to make those two id columns a primary key. This commit eliminates the warnings for join tables that have this setup. ManageIQ/manageiq#6713
* Define `:foreign_type` as a valid option in `SingularAssociation`yui-knk2016-01-193-3/+3
| | | | | `:foreign_type` is a valid option for `belongs_to` and `has_one` so remove this to `SingularAssociation`.
* Make sure we touch all the parents when touch_later.Arthur Neves2015-12-061-2/+1
| | | | | | | | | | | | | | | | | | | | The problem was that when saving an object, we would call touch_later on the parent which wont be saved immediteally, and it wont call any callbacks. That was working one level up because we were calling touch, during the touch_later commit phase. However that still didnt solve the problem when you have a 3+ levels of parents to be touched, as calling touch would affect the parent, but it would be too late to run callbacks on its grand-parent. The solution for this, is instead, call touch_later upwards when the first touch_later is called. So we make sure all the timestamps are updated without relying on callbacks. This also removed the hard dependency BelongsTo builder had with the TouchLater module. So we can still have the old behaviour if TouchLater module is not included. [fixes 5f5e6d924973003c105feb711cefdb726f312768] [related #19324]
* Update belongs_to.rbEhsan Yousefi2015-11-081-2/+1
| | | There is no need to to assign reflection name to a variable, because it's called once.
* Ensure `has_and_belongs_to_many` works with `belongs_to_required_by_default`Sean Griffin2015-10-291-2/+2
| | | | | | | | | | | | | Before this commit, if `ActiveRecord::Base.belongs_to_required_by_default` is set to `true`, then creating a record through `has_and_belongs_to_many` fails with the cryptic error message `Left side must exist`. This is because `inverse_of` isn't working properly in this case, presumably since we're doing trickery with anonymous classes in the middle. Rather than following this rabbit hole to try and get `inverse_of` to work in a case that we know is not publicly supported, we can just turn off this validation to match the behavior of 4.2 and earlier.
* Merge pull request #19686 from tsun1215/index_errorsSean Griffin2015-10-261-1/+1
|\ | | | | | | | | | | Errors can be indexed with nested attributes Close #8638
| * Errors can be indexed with nested attributesMichael Probber2015-04-171-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `has_many` can now take `index_errors: true` as an option. When this is enabled, errors for nested models will be returned alongside an index, as opposed to just the nested model name. This option can also be enabled (or disabled) globally through `ActiveRecord::Base.index_nested_attribute_errors` E.X. ```ruby class Guitar < ActiveRecord::Base has_many :tuning_pegs accepts_nested_attributes_for :tuning_pegs end class TuningPeg < ActiveRecord::Base belongs_to :guitar validates_numericality_of :pitch end ``` - Old style - `guitar.errors["tuning_pegs.pitch"] = ["is not a number"]` - New style (if defined globally, or set in has_many_relationship) - `guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]` [Michael Probber, Terence Sun]
* | docs, add missing `:nodoc: for `Associations::Builder`. [ci skip]Yves Senn2015-10-137-9/+9
| | | | | | | | | | | | This class is only used internally. We should keep it out of public documentation. This patch adds nodoc for `ActiveRecord::Associations::Builder` and everything nested within.
* | use correct DB connection for generated HABTM tableMatt Hanlon2015-08-071-3/+7
| |
* | Fix counter_cache for polymorphic associationsStefan Kanev2015-07-191-4/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Also removes a false positive test that depends on the fixed bug: At this time, counter_cache does not work with polymorphic relationships (which is a bug). The test was added to make sure that no StaleObjectError is raised when the car is destroyed. No such error is currently raised because the lock version is not incremented by appending a wheel to the car. Furthermore, `assert_difference` succeeds because `car.wheels.count` does not check the counter cache, but the collection size. The test will fail if it is replaced with `car.wheels_count || 0`.
* | Rename association option :class to :anonymous_classAndrew White2015-04-212-2/+2
|/ | | | | | | | | | | | | In 1f006c an option was added called :class to allow passing anonymous classes to association definitions. Since using :class instead of :class_name is a fairly common typo even amongst experienced developers this can result in hard to debug errors arising in raise_on_type_mismatch? To fix this we're renaming the option from :class to :anonymous_class as that is a more correct description of what the option is for. Since this was an internal, undocumented option there is no need for a deprecation. Fixes #19659
* Batch touch parent recordsArthur Neves2015-04-081-6/+7
| | | | | | | | | | [fixes #18606] Make belongs_to use touch over touch_later when running the callbacks. Add more tests and small method rename Thanks Jeremy for the feedback.
* Freeze static arguments for gsubbrainopia2015-04-021-1/+1
|
* Prefer string patterns for gsubbrainopia2015-04-021-1/+1
| | | | | | | | | | | | | | | | | https://github.com/ruby/ruby/pull/579 - there is a new optimization since ruby 2.2 Previously regexp patterns were faster (since a string was converted to regexp underneath anyway). But now string patterns are faster and better reflect the purpose. Benchmark.ips do |bm| bm.report('regexp') { 'this is ::a random string'.gsub(/::/, '/') } bm.report('string') { 'this is ::a random string'.gsub('::', '/') } bm.compare! end # string: 753724.4 i/s # regexp: 501443.1 i/s - 1.50x slower
* Require `belongs_to` by default.Josef Šimánek2015-02-213-8/+26
| | | | Deprecate `required` option in favor of `optional` for belongs_to.
* Provide a better error message on :required associationHenrik Nygren2015-01-281-1/+1
| | | | Fixes #18696.
* Remove support to activerecord-deprecated_findersRafael Mendonça França2015-01-027-67/+45
|
* Add foreign_type option for polymorphic has_one and has_many.Ulisses Almeida + Kassio Borges2014-12-082-2/+2
| | | | | | | To be possible to use a custom column name to save/read the polymorphic associated type in a has_many or has_one polymorphic association, now users can use the option :foreign_type to inform in what column the associated object type will be saved.
* Allow habtm class_name option to be consistent with other associationsshiksha2014-11-131-1/+1
|
* Ensure HABTM relationships produce valid class names (Fixes #17119)Sammy Larbi2014-11-091-1/+1
|
* Change `gsub` to `tr` where possibleAlexander Balashov2014-10-061-1/+1
|
* Remove unused 1:1 association :remote optionChris Griego2014-07-161-1/+1
| | | This option is unused, left over from pre-1.0 Rails to internally distinguish the location of the foreign key.
* Add a `required` option to singular associationsSean Griffin2014-07-042-1/+13
| | | | | | | | | | | | | | | | | | | | | | In addition to defining the association, a `required` association will also have its presence validated. Before: ```ruby belongs_to :account validates_presence_of :account ``` After: ```ruby belongs_to :account, required: true ``` This helps to draw a distinction between types of validations, since validations on associations are generally for data integrity purposes, and aren't usually set through form inputs.
* Merge pull request #12450 from iantropov/masterRafael Mendonça França2014-06-273-5/+16
|\ | | | | | | | | | | | | | | | | Fix bug, when ':dependent => :destroy' violates foreign key constraints Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/builder/association.rb activerecord/lib/active_record/associations/builder/has_one.rb
| * Fix bug, when ':dependent => :destroy' option violates foreign key ↵Ivan Antropov2013-10-263-9/+18
| | | | | | | | constraints, issue #12380
* | Fix has_and_belongs_to_many in a namespaced model pointing to a non ↵Rafael Mendonça França2014-06-191-8/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | namespaced model Now the following case will work fine class Tag < ActiveRecord::Base end class Publisher::Article < ActiveRecord::Base has_and_belongs_to_many :tags end Fixes #15761
* | Remove a long gone :order option from has_one's valid_options.thedarkone2014-06-091-1/+1
| |
* | Merge pull request #15210 from arthurnn/fix_hbtm_reflectionArthur Neves2014-05-241-2/+2
| | | | | | | | | | | | | | | | | | Fix habtm reflection Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/counter_cache.rb activerecord/lib/active_record/reflection.rb activerecord/test/cases/reflection_test.rb
* | Prevented belongs_to: touch propagating up if there are no changes being savedBrock Trappitt2014-05-211-1/+1
| |
* | Fixed the inferred table name for HABTM within a schemaEric Chahin2014-05-201-1/+1
| | | | | | | | Fixes #14824.
* | Remove the assignment for real this time.Zachary Scott2014-05-161-2/+2
| | | | | | | | | | | | cc: https://github.com/rails/rails/commit/a11e2fcff8cdc9691b977d8330abe63024f8c732#commitcomment-6353614 and 0369dbf
* | prepend mutates the string so we can remove the assignmentRafael Mendonça França2014-05-161-1/+1
| |
* | Don't use Active Support where it is not needed.Rafael Mendonça França2014-05-141-1/+3
| | | | | | | | | | parent_name is a string of nil, never a blank string so we don't need to use presence.
* | Fix how to compute class name on habtm namespaced.Kassio Borges2014-05-131-1/+5
| | | | | | | | | | | | Thank's for @laurocaetano for the help with tests. :smiley: Fixes #14709
* | Fix custom join_table name on habtm reflectionsKassio Borges2014-04-251-1/+1
| | | | | | | | | | | | 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.
* | Hide BelongsToAssociation#increment_counters and #decrement_countersJean Boussier2014-04-151-1/+0
| |
* | Use inheritance chain instead of callbacks to increment counter caches after ↵Jean Boussier2014-04-141-16/+0
| | | | | | | | destroy
* | Use inheritance chain instead of callbacks to increment counter caches after ↵Jean Boussier2014-04-141-12/+1
| | | | | | | | create
* | Make counter cache decrementation on destroy idempotentJean Boussier2014-04-131-4/+5
| |
* | Associations now raise `ArgumentError` on name conflicts.Lauro Caetano2014-01-311-0/+6
| | | | | | | | | | Dangerous association names conflicts include instance or class methods already defined by `ActiveRecord::Base`.
* | On destroying do not touch destroyed belongs to association.Paul Nikitochkin2013-12-231-1/+1
| | | | | | | | Fixes: #13445