Rails_Adminインターフェイスのモデルのリストビューで関連付け全体を検索する方法を理解するのに問題があります。
背景情報:
- モデル:投稿、ユーザー、ハブ、および組織。
- 目的:ユーザーは、ハブデバイスを介してデータベースにhttpリクエストを投稿するデバイスを使用して投稿を行います。データベースには、ユーザーによる投稿が表示されます。投稿は、ハブに属するデバイスを介して投稿を送信するユーザーに属し、ハブは組織に属します。
- 達成したいこと:rails adminで、投稿モデルのカスタムフィールドに、投稿を作成したユーザーが属する組織を表示したいと思います。
- 現在の問題:Postモデルの組織メソッドを使用して、Postのユーザーの組織をフィールドに文字列として表示させることはできますが、Rails_adminでこれを検索可能にするフィルターを作成できません。Rails_Adminで、に行を追加し
searchable :organization
てconfig.model Post do...field :organization do{}end...end
も機能しません。
この件についてアドバイスをいただければ幸いです。ありがとうございました!
ポストモデル:
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :hub
attr_accessible :hub_id, :user_id
before_validation :set_count
validates :hub_id, :presence => true
validates :user_id, :presence => true
validates :count, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
before_create :update_redis
def organization
self.hub.organization.name
end
def user_email
self.user.email
end
private
def user_email
self.user.email
end
end
ユーザーモデル:
class User < ActiveRecord::Base
belongs_to :organization
has_many :posts, :extend => User::Posts
accepts_nested_attributes_for :organization
attr_accessible :email, :password, :password_confirmation, :remember_me, :as => [:default, :admin]
attr_accessible :device_id, :organization_id, :role_ids, :as => :admin
attr_accessible :organization_attributes, :as => [:admin, :manager]
after_save :update_total
end
ハブモデル:
class Hub < ActiveRecord::Base
devise :token_authenticatable, :trackable
belongs_to :organization, :inverse_of => :hubs
has_many :posts
attr_accessible :location, :organization_id, :as => :admin
validates :organization, :presence => true
validates :location, :presence => true
before_save :ensure_authentication_token
end
組織モデル:
class Organization < ActiveRecord::Base
attr_accessible :name, :street_address_1, :street_address_2, :city, :state, :zip_code, :goal_per_hour, :as => [:admin, :manager
]
has_many :hubs
has_many :posts, :through => :hubs, :extend => Organization::Posts
has_many :users
validates :name, :presence => true, :uniqueness => true
end
rails_admin.rb初期化子:
config.model Post do
list do
filters [:user, :hub]
items_per_page 200
field :id do
column_width 50
end
field :count do
column_width 35
end
field :user do
column_width 50
end
field :user_email
field :hub do
column_width 50
end
field :organization do
label "Organization"
# searchable :organization # This line doesn't work!!
end
field :created_at do
strftime_format "%m/%d/%y %H:%M"
column_width 90
end
#field :updated_at do
# strftime_format "%m/%d/%y %H:%M"
# column_width 90
#end
end
show do
include_all_fields
field :user_email
field :organization_name, :string do
label "Organization"
end
end
end