无法通过关联使用 has_many 写入未知属性 `followings_count`
can't write unknown attribute `followings_count` with has_many though association
虽然与 Rails 关联,但我正在尝试使用 has_many 5。保存“类别”时出现此错误。
ActiveModel::MissingAttributeError in CategoriesController#create
can't write unknown attribute followings_count
以下模特:
class Following < ApplicationRecord
belongs_to :category, touch: true, counter_cache: true
belongs_to :followed_category, counter_cache: :followers_count, class_name: 'Category'
end
类别模型:
class Category < ApplicationRecord
has_many :followings
has_many :followed_categories, through: :followings
has_many :followers, foreign_key: :followed_category_id, class_name: 'Following'
has_many :follower_categories, through: :followers, source: :category
end
类别控制器:
class CategoriesController < InheritedResources::Base
private
def category_params
params.require(:category).permit(:name, :description, :display_in_navbar, post_ids: [], followed_category_ids: [])
end
end
_form.html.rb
<%= simple_form_for(@category) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.association :followed_categories, as: :check_boxes %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
计数器缓存似乎正在寻找类别 table 上名为 followings_count
的列。当你在那里时,我猜你也可能失踪了 followers_count
。这帮助我更好地理解它 https://redpanthers.co/counter-cache-how-to-get-started/
虽然与 Rails 关联,但我正在尝试使用 has_many 5。保存“类别”时出现此错误。
ActiveModel::MissingAttributeError in CategoriesController#create
can't write unknown attribute
followings_count
以下模特:
class Following < ApplicationRecord
belongs_to :category, touch: true, counter_cache: true
belongs_to :followed_category, counter_cache: :followers_count, class_name: 'Category'
end
类别模型:
class Category < ApplicationRecord
has_many :followings
has_many :followed_categories, through: :followings
has_many :followers, foreign_key: :followed_category_id, class_name: 'Following'
has_many :follower_categories, through: :followers, source: :category
end
类别控制器:
class CategoriesController < InheritedResources::Base
private
def category_params
params.require(:category).permit(:name, :description, :display_in_navbar, post_ids: [], followed_category_ids: [])
end
end
_form.html.rb
<%= simple_form_for(@category) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.association :followed_categories, as: :check_boxes %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
计数器缓存似乎正在寻找类别 table 上名为 followings_count
的列。当你在那里时,我猜你也可能失踪了 followers_count
。这帮助我更好地理解它 https://redpanthers.co/counter-cache-how-to-get-started/