Closes [#49574][]
Issue #49574 outlines how an Active Record join model accessed through a
`has_many ..., through:` association is unable to infer an appropriate
`:inverse_of` association by pluralizing a predictably pluralizable
class name.
This commit resolves that issue by also checking a model's reflections
for a pluralized inverse name in addition to whatever's provided through
the `:as` option or inferred in the singular.
The issue shares a code sample:
```ruby
ActiveRecord::Schema.define do
create_table "listings", force: :cascade do |t|
t.bigint "list_id", null: false
t.bigint "pin_id", null: false
end
create_table "lists", force: :cascade do |t|
end
create_table "pins", force: :cascade do |t|
end
end
class Pin < ActiveRecord::Base
has_many :listings
end
class List < ActiveRecord::Base
has_many :listings
has_many :pins, through: :listings
end
class Listing < ActiveRecord::Base
belongs_to :list
belongs_to :pin
end
class BugTest < Minitest::Test
def test_association_stuff
list = List.create!
pin = list.pins.new
pin.save!
assert_equal [pin], list.reload.pins
assert_equal 1, list.reload.pins.count
end
end
```
Unfortunately, there isn't a one-to-one mapping in the test suite's
`test/model` directory for this type of structure. The most similar
associations were between the `Book`, `Subscription`, and `Subscriber`.
For the sake of ease, this commit wraps the test block in a new
`skipping_validations_for` helper method to ignore validations declared
on the `Subscription` join table model.
[#49574]: https://github.com/rails/rails/issues/49574
Following the discussion in #50770, the new format will be:
`[dasherized-app-name]([colorized-env])>`
For example, if the app's module name is `MyApp`, the prompt will be:
`my-app(dev)>`, where the `dev` part will be colored.
Update railties/lib/rails/commands/console/console_command.rb
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
Update changelog
Setting `TERM=dumb` is enough to disable colorization in those tests.
Mocked app should also load console helpers
Move console method extension to IRBConsole's constructor
Test IRB autocompletion control without mutating ENV
Control IRB autocompletion without mutating ENV
Correct local variable name
Avoid corrupting the cache by mutating the return value, and also
sligthly reduce memory usage when the quoting format often return
an unmodified string.
- This is a fix needed to unblock
https://github.com/rails/rails/pull/50284,
because Active Storage relies on a Active Record bug.
The problem is best understood with a small snippet:
```
blob = ActiveStorage::Blob.new
ActiveStorage::Attachment.new(blob: blob)
ActiveStorage::Attachment.new(blob: blob)
# Currently:
p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy []>
# Once the Active Record bug is fixed:
p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy [#<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>, #<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>]>
# Trying to save the blob would result in trying to create 2 attachments which
# fails because of unique constrainsts.
```
### Code path
The real code path that does what the snippet above does is located here:
9c3ffab47c/activestorage/lib/active_storage/attached/many.rb (L52)
It's basically doing this:
```
user.images.attach "photo1.png"
# Initialize a Blob record and an Attachment
user.images.attach "photo2.png"
# Get the Blob from above, create another Attachment
# Initialize a new Blob record and an new Attachment
# rinse and repeat every time `attach` is called
```
Basically each time we call `attach`, we grab the previous blobs that were attached
(and that already have an Attachment record), and
### Solution
- Explicitly set the `inverse_of`, so that it behaves as if #50284 is shipped
- Don't build a new attachment for blob already having one.
### Tests
I didn't add tests, the test suite is already well covered, adding the `inverse_of`
without any changes breaks the test suite already. You can try by running
for instance the `activestorage/test/models/attached/many_test.rb`.
When Rake parses an argument string with no tasks, it sets the top-level
task as "default". Prior to this commit, if no default task was defined
(for example, if an app was generated with `--skip-test` and didn't
define its own default task), `Rails::Command::RakeCommand` would raise
`UnrecognizedCommandError`, preventing Rake from displaying the task
list.
This commit changes `Rails::Command::RakeCommand` to let Rake handle the
"default" task.
Fixes#50700.
Prior to this commit, `bin/rails` would pass unrecognized bare options
on to Rake:
```console
$ bin/rails -v
Rails 7.2.0.alpha
$ bin/rails -V
rake, version 13.0.6
$ bin/rails -s
Running 0 tests in a single process (parallelization threshold is 50)
...
$ bin/rails -S
invalid option: -S
```
This commit changes `bin/rails` to print its help message when given an
unrecognized bare option:
```console
$ bin/rails -v
Rails 7.2.0.alpha
$ bin/rails -V
Usage:
bin/rails COMMAND [options]
You must specify a command. The most common commands are:
...
$ bin/rails -s
Usage:
bin/rails COMMAND [options]
You must specify a command. The most common commands are:
...
$ bin/rails -S
Usage:
bin/rails COMMAND [options]
You must specify a command. The most common commands are:
...
```
However, for backward compatibility, an exception has been made for the
`-T` / `--tasks` option:
```console
$ bin/rails -T
# Prints list of Rake tasks...
```
Addresses #50712.
Prior 7.1 Rails always included `primary_key` in `RETURNING` clause on
record creation. This was changed in 7.1 to include more auto-populated
columns if such columns exist. This change lead to situations where
no columns were requested in `RETURNING` clause, even the `primary_key`.
This change brings back the old behavior of always requesting the
`primary_key` in `RETURNING` clause if no other columns are requested.
Given that the limiter implementation provided by Kredis is a simple
increment with a limit, all `ActiveSupport::Cache` already provide that
same capability, with a wide range of backing stores, and not just Redis.
This even allow to use SolidCache has a backend if you so desire.
If we feel particularly fancy, we could also accept a more generic
limiter interface to better allow users to swap the implementation
for better algorithms such as leaky-bucket etc.
MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression. This commit fixes the escaping of the default value for such expressions if a single quote is included.
See the following for more: https://dev.mysql.com/blog-archive/generated-columns-in-mysql-5-7-5/