Refactor #34569
Postpone LookupValue match validations
Description
match gets overriden by Rails if the record has been build by association e.g. `host.lookup_values.build()`
That basically means that any assigned value to match prior save is irrelevant because it will be replaced by Rails.
This was never an issue, because Rails just replaced the value with the same value as we've assigned, so it went unnoticed.
It only starts to cause issues once Rails had fixed a bug where it wrongly allowed foreign_key to be passed to <assoc>.build(foreign_key => value)
This is for sym vs string in hash keys as in https://github.com/rails/rails/blob/13cdd7a6aef86b23c3410adff1d4f6eeefc72c67/activerecord/lib/active_record/associations/association.rb#L196 reflection.foreign_key returned a symbol, but attribute is a string, so the assigned foreign key did not get filtered out properly.
This bug had been fixed in Rails 6.1 and the assigned foreign key is filtered out properly and our validations fail on presence of match as that gets assigned from host only after the Host is saved and thats after validation.
Related issues
Associated revisions
History
#1
Updated by The Foreman Bot about 1 year ago
- Assignee set to Ondřej Ezr
- Status changed from New to Ready For Testing
- Pull request https://github.com/theforeman/foreman/pull/9137 added
#2
Updated by Ondřej Ezr about 1 year ago
- Parent task set to #28570
#3
Updated by Ondřej Ezr about 1 year ago
- Parent task deleted (
#28570)
#4
Updated by Ondřej Ezr about 1 year ago
- Blocks Tracker #28570: Rails 6.1 Tracker added
#5
Updated by The Foreman Bot about 1 year ago
- Fixed in Releases 3.3.0 added
#6
Updated by Ondřej Ezr about 1 year ago
- Status changed from Ready For Testing to Closed
Applied in changeset foreman|5a77b476fca4ae005b0edfe15647f55ce7a42ad5.
Fixes #34569 - postpone LookupValue#match validations
Validation for presence of foreign_key has to be postponed because of Rails internals.
association.build() filters out foreign_key for given association because it assigns the value for that key after the main record gets saved.
This filtering did not work properly until Rails 6.1, thus we did not notice and thought it works.
Since Rails 6.1 https://github.com/rails/rails/blob/13cdd7a6aef86b23c3410adff1d4f6eeefc72c67/activerecord/lib/active_record/associations/association.rb#L196 works properly
and returns `foreing_key` in string format and thus filters out attributes from hashes where those passed as string keys.
All other validations can be run during validation if those allow nil value as the Rails assigned key should not need validation.