从学校视图中搜索不同 类 的所有学生
Search for all students in different classes from school view
我有这样的模型结构:
City has_many School has_many Sclasses has_many Users
我想做的是从 school#show 视图中搜索当前学校的所有用户。过去,我用这个来搜索它们:
@search = @school.users.ransack(params[:q])
现在,User 模型的位置已经更改,我不知道如何正确地执行此操作。我接下来要做什么:
def show
find_city
find_school
# search variable for all users in this school
if (params.has_key?(:q))
# search for all users in this school
else
# show all classes of this school
@sclasses = @school.sclasses.all
end
end
学校模式
class School < ActiveRecord::Base
belongs_to :city
has_many :sclasses
validates :name, presence: true
validates :icon_url, length: { maximum: 100 }
end
S级模型
class Sclass < ActiveRecord::Base
belongs_to :school
has_many :users
validates :name, presence: true, numericality: { only_integer: true }
validates :icon_url, length: { maximum: 100 }
end
routes.rb
...
get "/gradovi" => "welcome#index", as: "cities"
get "/gradovi/novi" => "cities#new", as: "new_city"
post "/gradovi" => "cities#create"
get "/gradovi/:id" => "cities#show", as: "city"
get "/gradovi/:id/uredi" => "cities#edit", as: "edit_city"
patch "/gradovi/:id" => "cities#update"
get "/gradovi/:city_id" => "schools#index", as: "schools"
get "/gradovi/:city_id/nova-skola" => "schools#new", as: "new_school"
post "/gradovi/:city_id/" => "schools#create"
get "/gradovi/:city_id/skola/:id" => "schools#show", as: "school"
get "/gradovi/:city_id/skola/:id/uredi" => "schools#edit", as: "edit_school"
patch "/gradovi/:city_id/skola/:id" => "schools#update"
get "/gradovi/:city_id/skola/:school_id" => "sclasses#index", as: "sclasses"
post "/gradovi/:city_id/skola/:school_id" => "sclasses#create"
get "/gradovi/:city_id/skola/:school_id/razred/dodaj" => "sclasses#new", as: "new_sclass"
get "/gradovi/:city_id/skola/:school_id/razred/:id" => "sclasses#show", as: "sclass"
get "/gradovi/:city_id/skola/:school_id/razred/:id/uredi" => "sclasses#edit", as: "edit_sclass"
patch "/gradovi/:city_id/skola/:school_id/razred/:id" => "sclasses#update"
get "/gradovi/:city_id/skola/:school_id/razred/:sclass_id" => "users#index", as: "users"
post "/gradovi/:city_id/skola/:school_id/razred/:sclass_id" => "users#create"
get "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id" => "users#show", as: "user"
get "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id/uredi" => "users#edit", as: "edit_user"
patch "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id" => "users#update"
delete "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id" => "users#destroy"
resources :city
end
有了 @school
变量,您可以通过以下方式获取与学校关联的所有用户的列表:
User.joins(sclass: :school).where(schools: { id: @school.id })
或者,您可以简单地定义一个关联:
class School < ActiveRecord::Base
has_many :users, through: :sclasses
end
现在,在视图中只需:
@school.users
我有这样的模型结构:
City has_many School has_many Sclasses has_many Users
我想做的是从 school#show 视图中搜索当前学校的所有用户。过去,我用这个来搜索它们:
@search = @school.users.ransack(params[:q])
现在,User 模型的位置已经更改,我不知道如何正确地执行此操作。我接下来要做什么:
def show
find_city
find_school
# search variable for all users in this school
if (params.has_key?(:q))
# search for all users in this school
else
# show all classes of this school
@sclasses = @school.sclasses.all
end
end
学校模式
class School < ActiveRecord::Base
belongs_to :city
has_many :sclasses
validates :name, presence: true
validates :icon_url, length: { maximum: 100 }
end
S级模型
class Sclass < ActiveRecord::Base
belongs_to :school
has_many :users
validates :name, presence: true, numericality: { only_integer: true }
validates :icon_url, length: { maximum: 100 }
end
routes.rb
...
get "/gradovi" => "welcome#index", as: "cities"
get "/gradovi/novi" => "cities#new", as: "new_city"
post "/gradovi" => "cities#create"
get "/gradovi/:id" => "cities#show", as: "city"
get "/gradovi/:id/uredi" => "cities#edit", as: "edit_city"
patch "/gradovi/:id" => "cities#update"
get "/gradovi/:city_id" => "schools#index", as: "schools"
get "/gradovi/:city_id/nova-skola" => "schools#new", as: "new_school"
post "/gradovi/:city_id/" => "schools#create"
get "/gradovi/:city_id/skola/:id" => "schools#show", as: "school"
get "/gradovi/:city_id/skola/:id/uredi" => "schools#edit", as: "edit_school"
patch "/gradovi/:city_id/skola/:id" => "schools#update"
get "/gradovi/:city_id/skola/:school_id" => "sclasses#index", as: "sclasses"
post "/gradovi/:city_id/skola/:school_id" => "sclasses#create"
get "/gradovi/:city_id/skola/:school_id/razred/dodaj" => "sclasses#new", as: "new_sclass"
get "/gradovi/:city_id/skola/:school_id/razred/:id" => "sclasses#show", as: "sclass"
get "/gradovi/:city_id/skola/:school_id/razred/:id/uredi" => "sclasses#edit", as: "edit_sclass"
patch "/gradovi/:city_id/skola/:school_id/razred/:id" => "sclasses#update"
get "/gradovi/:city_id/skola/:school_id/razred/:sclass_id" => "users#index", as: "users"
post "/gradovi/:city_id/skola/:school_id/razred/:sclass_id" => "users#create"
get "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id" => "users#show", as: "user"
get "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id/uredi" => "users#edit", as: "edit_user"
patch "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id" => "users#update"
delete "/gradovi/:city_id/skola/:school_id/razred/:sclass_id/ucenik/:id" => "users#destroy"
resources :city
end
有了 @school
变量,您可以通过以下方式获取与学校关联的所有用户的列表:
User.joins(sclass: :school).where(schools: { id: @school.id })
或者,您可以简单地定义一个关联:
class School < ActiveRecord::Base
has_many :users, through: :sclasses
end
现在,在视图中只需:
@school.users