1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/models
Volmer Campos Soares 4e37f28881 Derive foreign key from model name in has_many associations
Currently Active Record derives the foreign key name for `has_many`
associations based on the Active Record Base `name. Sometimes when using
additional conventions for class names, such as with class name suffixes
and prefixes, it makes sense to be able to further customize the
foreign key logic to account for a different pattern.

Similar to what was done in the
[case of table names](https://github.com/rails/rails/pull/42213),
as Active Record Base extends Active Model Naming, we have already a
`model_name` at the class-level that behaves similarly to `name` while
at the same time giving more flexibility by allowing specifying a custom
name and namespacing.

This commit changes the foreign key computation in Reflections to infer
its value based on the Active Record's `model_name` object instead of
its class `name`. This allows customization of the foreing key pattern
since a distinct instance of `ActiveModel::Name` can be used instead.

For example, to use `post_id` as foreign key for a `PostRecord` class
in a `has_many` association:

```ruby
 class PostRecord < ActiveRecord::Base
   has_many :comments

   class << self
     def model_name
       ActiveModel::Name.new(self, nil, "Post")
     end
   end
 end

 PostRecord.reflect_on_association(:comments).foreign_key
 # => "post_id"
```
2021-05-12 15:21:31 -04:00
..
admin Update prefix and allow suffix options for store accessors 2018-06-12 07:10:09 -07:00
autoloadable
publisher
account.rb Ensure Contextual validations fire on associations 2019-10-04 12:17:53 +01:00
admin.rb
aircraft.rb
arunit2_model.rb
author.rb Add option to skip joins for associations. 2021-04-19 11:17:31 -04:00
author_encrypted.rb Add support for uniqueness validations 2021-04-01 15:02:15 +02:00
auto_id.rb
binary.rb
binary_field.rb Handle binary strings in Active Record serialized columns 2020-10-14 11:57:34 +02:00
bird.rb Revert "Merge pull request #35186 from kamipo/fix_leaking_scope_on_relation_create" 2019-02-15 12:06:45 +09:00
book.rb Extract encrypted models to their own files 2021-04-01 15:02:14 +02:00
book_destroy_async.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
book_encrypted.rb Add new "encrypted_books" table to the schema 2021-04-03 08:00:01 -04:00
boolean.rb
bulb.rb Implicit scoping does no longer leak scope to class level querying methods 2020-10-30 00:25:38 +00:00
cake_designer.rb
car.rb Fix unscoping association scope on joins not to raise an error 2020-04-16 00:34:41 +09:00
carrier.rb
cart.rb Don't over protect the MySQL users on insert_all 2020-11-04 20:26:22 +00:00
cat.rb
categorization.rb
category.rb Remove unused Category.has_and_belongs_to_many :popular_grouped_posts definition 2021-04-12 15:05:42 +09:00
chef.rb
citation.rb Fix has_many_inversing with touch: true on a belongs_to association with inverse 2021-01-07 22:15:24 +00:00
club.rb chore: fix spelling change favourite to the more used favorite 2021-04-12 12:35:12 +10:00
college.rb Removes require_dependency from the AR test suite 2020-05-06 09:01:38 +02:00
column.rb
column_name.rb
comment.rb Add option to skip joins for associations. 2021-04-19 11:17:31 -04:00
company.rb Avoid stack level too deep in predicate builder 2021-02-11 11:32:20 -05:00
company_in_module.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
computer.rb
contact.rb Deduplicate various Active Record schema cache structures 2019-06-03 13:31:42 +02:00
content.rb
contract.rb Fix complicated has_many through with nested where condition 2021-01-05 11:40:04 +09:00
country.rb create_table with :primary_key option has no effect if id: false is given 2018-10-27 22:57:16 +09:00
course.rb Removes require_dependency from the AR test suite 2020-05-06 09:01:38 +02:00
customer.rb Fix composed_of with symbol mapping 2020-12-15 20:06:23 +09:00
customer_carrier.rb
dashboard.rb Revert "Refactor uncastable through reflection test to detect join key overrides" 2020-09-25 17:49:52 -04:00
default.rb
department.rb
destroy_async_parent.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
destroy_async_parent_soft_delete.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
developer.rb Handle false in relation strict loading checks 2021-03-16 16:26:59 -04:00
discount.rb Reproduce the preloader regression in #41596 2021-03-15 12:36:21 -07:00
dl_keyed_belongs_to.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
dl_keyed_belongs_to_soft_delete.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
dl_keyed_has_many.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
dl_keyed_has_many_through.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
dl_keyed_has_one.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
dl_keyed_join.rb Destroy associations in a background job. 2020-09-24 14:24:15 -04:00
dog.rb
dog_lover.rb
doubloon.rb
drink_designer.rb Polymorphic has_one touch: Reset association cache result after create transaction 2019-07-31 05:59:23 +02:00
edge.rb
electron.rb
engine.rb
entrant.rb
entry.rb Add delegated type to Active Record (#39341) 2020-05-23 15:14:40 -07:00
essay.rb
essay_destroy_async.rb Handle STI models for has_many dependent: :destroy_async 2021-01-06 02:04:23 +01:00
event.rb
eye.rb
face.rb Set inverse during has one autosave if necessary 2020-10-30 15:31:40 +00:00
family.rb
family_tree.rb
friendship.rb
frog.rb Merge pull request #30956 from CJStadler/with-lock-changed-deprecation 2018-03-28 13:11:01 -04:00
guid.rb
guitar.rb
hotel.rb
human.rb Set inverse during has one autosave if necessary 2020-10-30 15:31:40 +00:00
image.rb :polymorphic, :as, and :foreign_type are valid for polymorphic association 2019-11-10 10:54:58 +09:00
interest.rb Replace test Man with Human 2020-08-14 11:37:09 -04:00
invoice.rb Deprecates Enumerable#sum and Array#sum 2021-05-03 10:26:39 -07:00
item.rb
job.rb
joke.rb
keyboard.rb
legacy_thing.rb
lesson.rb
line_item.rb Reproduce the preloader regression in #41596 2021-03-15 12:36:21 -07:00
liquid.rb
matey.rb
measurement.rb This PR adds support to retrieve partitioned indexes when asking for 2020-02-06 07:36:44 +02:00
member.rb Add has_one through disable_joins 2021-05-07 09:00:42 -04:00
member_detail.rb Using existing models for building multiple has_one through tests 2018-04-22 04:16:44 +09:00
member_type.rb
membership.rb
mentor.rb
message.rb Add delegated type to Active Record (#39341) 2020-05-23 15:14:40 -07:00
minimalistic.rb
minivan.rb
mixed_case_monkey.rb Replace test Man with Human 2020-08-14 11:37:09 -04:00
molecule.rb
mouse.rb Don't validate non dirty association targets 2019-07-15 18:21:20 +01:00
movie.rb
node.rb
non_primary_key.rb
notification.rb
numeric_data.rb Fix read_attribute_before_type_cast to consider attribute aliases 2020-10-18 20:32:47 -03:00
order.rb
organization.rb
other_dog.rb Removes require_dependency from the AR test suite 2020-05-06 09:01:38 +02:00
owner.rb
parrot.rb Don't like to add extra models/tables/fixtures, use existing those 2021-02-25 11:32:44 +09:00
person.rb chore: fix spelling change favourite to the more used favorite 2021-04-12 12:35:12 +10:00
personal_legacy_thing.rb
pet.rb
pet_treasure.rb
pirate.rb Ensure Contextual validations fire on associations 2019-10-04 12:17:53 +01:00
possession.rb
post.rb Derive foreign key from model name in has_many associations 2021-05-12 15:21:31 -04:00
post_encrypted.rb Extract encrypted models to their own files 2021-04-01 15:02:14 +02:00
price_estimate.rb Fix numericality validator not to be affected by custom getter 2018-08-13 23:28:46 +09:00
professor.rb Removes require_dependency from the AR test suite 2020-05-06 09:01:38 +02:00
project.rb
publisher.rb
randomly_named_c1.rb
rating.rb Maintain extra joins for string or complex arel conditions 2019-10-04 13:29:18 +09:00
reader.rb
recipe.rb
record.rb
reference.rb chore: fix spelling change favourite to the more used favorite 2021-04-12 12:35:12 +10:00
reply.rb Allow attribute aliases for validates_uniqueness_of 2020-06-01 22:44:39 +09:00
room.rb Fixed odd behavior of inverse_of with multiple belongs_to to same class 2020-11-21 02:06:21 +09:00
section.rb Fix circular autosave: true 2018-07-23 14:53:27 -07:00
seminar.rb Fix circular autosave: true 2018-07-23 14:53:27 -07:00
session.rb Fix circular autosave: true 2018-07-23 14:53:27 -07:00
ship.rb Ensure Contextual validations fire on associations 2019-10-04 12:17:53 +01:00
ship_part.rb
shipping_line.rb Reproduce the preloader regression in #41596 2021-03-15 12:36:21 -07:00
shop.rb
shop_account.rb
speedometer.rb
sponsor.rb
squeak.rb Don't validate non dirty association targets 2019-07-15 18:21:20 +01:00
strict_zine.rb Ignore strict loading violations on instances loaded through fixtures 2020-12-10 15:23:14 -06:00
string_key_object.rb
student.rb
subscriber.rb Revert "Refactor uncastable through reflection test to detect join key overrides" 2020-09-25 17:49:52 -04:00
subscription.rb Revert "Refactor uncastable through reflection test to detect join key overrides" 2020-09-25 17:49:52 -04:00
tag.rb
tagging.rb
task.rb
topic.rb Implicit scoping does no longer leak scope to class level querying methods 2020-10-30 00:25:38 +00:00
toy.rb Fix has_many_inversing with touch: true on a belongs_to association with inverse 2021-01-07 22:15:24 +00:00
traffic_light.rb Extract encrypted models to their own files 2021-04-01 15:02:14 +02:00
traffic_light_encrypted.rb Extract encrypted models to their own files 2021-04-01 15:02:14 +02:00
treasure.rb
treaty.rb create_table with :primary_key option has no effect if id: false is given 2018-10-27 22:57:16 +09:00
tree.rb
tuning_peg.rb
tyre.rb
user.rb Fixed odd behavior of inverse_of with multiple belongs_to to same class 2020-11-21 02:06:21 +09:00
user_with_invalid_relation.rb compute_class raise when klass is not ActiveRecord::Base subclass 2020-12-14 14:49:14 +01:00
uuid_child.rb
uuid_comment.rb Allow delegated_type to be specified primary_key and foreign_key 2020-12-18 15:06:16 +09:00
uuid_entry.rb Allow delegated_type to be specified primary_key and foreign_key 2020-12-18 15:06:16 +09:00
uuid_item.rb
uuid_message.rb Allow delegated_type to be specified primary_key and foreign_key 2020-12-18 15:06:16 +09:00
uuid_parent.rb
vegetables.rb move ActiveRecord::Persistance#becomes logic into initialize block 2020-09-20 10:24:19 -06:00
vertex.rb
warehouse_thing.rb
wheel.rb Fix touch option to behave consistently with Persistence#touch method 2018-06-18 19:08:41 +09:00
without_table.rb
zine.rb Ignore strict loading violations on instances loaded through fixtures 2020-12-10 15:23:14 -06:00