0
0
Fork 0

Required foreign keys (#2003)

* Add `required: true` option to foreign column

* Fixes NoMethodError

```
> Favourite.new.valid?
NoMethodError: undefined method `reblog?' for nil:NilClass
```
This commit is contained in:
alpaca-tc 2017-04-17 22:54:33 +09:00 committed by Eugen
parent f8546ad5a2
commit 630de52fdd
5 changed files with 8 additions and 11 deletions

View file

@ -3,14 +3,14 @@
class Favourite < ApplicationRecord
include Paginable
belongs_to :account, inverse_of: :favourites
belongs_to :status, inverse_of: :favourites, counter_cache: true
belongs_to :account, inverse_of: :favourites, required: true
belongs_to :status, inverse_of: :favourites, counter_cache: true, required: true
has_one :notification, as: :activity, dependent: :destroy
validates :status_id, uniqueness: { scope: :account_id }
before_validation do
self.status = status.reblog if status.reblog?
self.status = status.reblog if status&.reblog?
end
end