我需要按名称对查询结果进行分组,并显示每个名称的结果数量.
控制器:
def show
@task = Task.where(:name => params[:name])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @task }
end
end
Run Code Online (Sandbox Code Playgroud)
我需要定义输出如下内容的东西:
show.html.erb:
Name | # of Tasks
Alan | 2
Bob | 1
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?谢谢.
我有一个看起来像这样的哈希:
extras = {'facebook' => '', 'twitter' => ''}
Run Code Online (Sandbox Code Playgroud)
我的用户属性是这样的:
class User < ActiveRecord::Base
attr_accessible :facebook, :twitter, #...
Run Code Online (Sandbox Code Playgroud)
我想以下列方式检索我的extras哈希中定义的数据(或者更好的oen - 这是我想到的那个)
extras.each do |k,v|
extras[k] = @user.k
end
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?
如何在Rails中捕获ActionView :: MissingTemplate错误?
例如,我PeopleController
有一个Index方法,但它不需要模板.当有人浏览root_url/people时,他们会获得默认的静态错误模板.
而且这不是唯一有问题的控制器; 我希望所有丢失的模板错误将用户重定向到我的自定义视图.
Rails版本 - 3.0.19
提前致谢!
所以我试图在我的用户中为它的角色和它的plan_id更新一些东西..我在控制台中想出了一些脏东西,但每次我都试图使用update_all类似的方式我没有在哪里.我想我错过了什么......这是我原来的控制台方式;
expired_subscription_user_ids = Subscription.where("expiry_date < ?", Time.now.beginning_of_day).pluck(:user_id)
User.where(:id => cancelled_subscription).each do |user|
user.role = 'cancelled'
user.plan_id = 'cancelled'
user.save
end
Run Code Online (Sandbox Code Playgroud)
这是同样的事情,但使用更新所有不适合我.
User.where(:id => cancelled_subscription).each do |user|
user.update_all(:role => 'subscriber', :plan_id => 'subscriber')
end
Run Code Online (Sandbox Code Playgroud)
因此,几乎所有使用cancelled_subscription的用户都会有自己的角色和plan_id.
这是一个示例哈希和一个在哈希中搜索的示例数组:
nicknames = { "Black Mamba" => "Kobe Bryant",
"Half Man Half Amazing" => "Vince Carter",
"The Big Fundamental" => "Tim Duncan",
"Big Ticket" => "Kevin Garnett",
"Obi-Wan Ginobili" => "Manu Ginobili",
"The Answer" => "Allen Iverson" }
names = [ "Vince Carter", "Manu Ginobili", "Allen Iverson" ]
Run Code Online (Sandbox Code Playgroud)
我想回来:
selected = { "Half Man Half Amazing" => "Vince Carter", "Obi-Wan Ginobili" => "Manu Ginobili", "The Answer" = "Allen Iverson" }
Run Code Online (Sandbox Code Playgroud)
这样做的好方法是什么?谢谢!