Ruby on rails 3.2 过滤有很多关系形式
我有一个简单的表格来显示一个与很多关系的模型Ruby on rails 3.2 过滤有很多关系形式,ruby-on-rails-3.2,activeadmin,Ruby On Rails 3.2,Activeadmin,我有一个简单的表格来显示一个与很多关系的模型 form do |f| f.semantic_errors *f.object.errors.keys f.inputs do f.has_many :messages, allow_destroy: true, heading: 'Messages' do |f| f.input :title end end f.actions end 我的问题是:如何在显示
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.has_many :messages, allow_destroy: true, heading: 'Messages' do |f|
f.input :title
end
end
f.actions
end
我的问题是:如何在显示发件人之前对消息进行预筛选?
这取决于当前的管理员用户。永远不要
我的解决方案是基于创建一个与模型条件有很多关系的自定义模型。
例如:
文件/app/models/key.rb
class Key < ActiveRecord::Base
attr_accessible :name,
:messages, :messages_attributes,
:messages_for_organization_attributes
has_many :messages, dependent: :destroy
accepts_nested_attributes_for :messages, allow_destroy: true
has_many :messages_for_organization, class_name: 'Message', conditions: proc {"organization_id" = #{$current_admin_user.organization_id}"}
accepts_nested_attributes_for :messages_for_organization, allow_destroy: true
end
class Message < ActiveRecord::Base
attr_accessible :title,
:key, :key_id
:organization, :organization_id
belongs_to :key
belongs_to :organization
end
是具有多条消息的模型AdminUser吗?不,不是。这有关系吗?
controller do
def edit
if current_admin_user.role?('super_admin')
@key = Key.find_by_id(params[:id])
else
@key = Key.joins('LEFT JOIN messages ON messages.key_id = key.id').where(id: params[:id]).first
end
$current_admin_user = current_admin_user # Important assignment to get access current_admin_user in Key model class
edit!
end
end
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
if current_admin_user.role?('super_admin')
f.has_many :messages, allow_destroy: true, heading: 'Messages' do |f|
f.input :title
end
else
f.has_many :messages_for_organization, allow_destroy: true, heading: 'Messages' do |f|
f.input :title
end
end
end
f.actions
end