具有自定义收集顺序的 ActiveAdmin has_many
ActiveAdmin has_many with custom collection order
给定以下模型:
class WorkoutProgram < ApplicationRecord
has_many :workouts, dependent: :destroy
end
class Workout < ApplicationRecord
belongs_to :workout_program
end
有什么方法可以在 ActiveAdmin has_many 输入中设置自定义顺序吗?我不想启用拖放功能,所以 sortable
不是一个选项。这个想法是按照编辑 WorkoutProgram
.
时添加的相同顺序显示锻炼
作为 解决方法 我在 WorkoutProgram
中使用另一个 has_many 关系只是为了这个目的。所以我在模型中添加了:
has_many :ordered_workouts, -> { ordered_by_id },
class_name: Workout.to_s, dependent: :destroy, inverse_of: :workout_program
然后像这样在 ActiveAdmin 页面中使用它:
form.has_many :ordered_workouts, allow_destroy: true do...
你可以这样做:
form.has_many :workouts, for: [:workouts, form.object.workouts.ordered_by_id] #, ...
给定以下模型:
class WorkoutProgram < ApplicationRecord
has_many :workouts, dependent: :destroy
end
class Workout < ApplicationRecord
belongs_to :workout_program
end
有什么方法可以在 ActiveAdmin has_many 输入中设置自定义顺序吗?我不想启用拖放功能,所以 sortable
不是一个选项。这个想法是按照编辑 WorkoutProgram
.
作为 解决方法 我在 WorkoutProgram
中使用另一个 has_many 关系只是为了这个目的。所以我在模型中添加了:
has_many :ordered_workouts, -> { ordered_by_id },
class_name: Workout.to_s, dependent: :destroy, inverse_of: :workout_program
然后像这样在 ActiveAdmin 页面中使用它:
form.has_many :ordered_workouts, allow_destroy: true do...
你可以这样做:
form.has_many :workouts, for: [:workouts, form.object.workouts.ordered_by_id] #, ...