I have a task to list students' details. I want to perform searching based on name and city. If cityId==0 and name='', then I want to list all my student details. How can I do this? I did this in a wrong way. The controller is:
if(Student.where(params[:cityId])==0)
studentcount = Student.count()
#students = Student.limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order(params[:jtSorting])
#jtable = {'Result' => 'OK','Records' => #students.map(&:attributes), :TotalRecordCount => studentcount}
else
studentcount = Student.where("name LIKE ? AND city = ?", "%#{params[:name]}%", params[:cityId]).count()
#students = Student.where("name LIKE ? AND city = ?", "%#{params[:name]}%", params[:cityId]).limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order(params[:jtSorting])
#jtable = {'Result' => 'OK','Records' => #students.map(&:attributes), :TotalRecordCount => studentcount}
Your condition should look like this:
if( Student.where(:cityId => params[:cityId]).count == 0 )
The if-statement you have tests a ActiveRecord::Relation and an Integer for equality which will never be true
if User.where(city_id: params[:cityId]).empty?
Related
#baseball_games = 0
#basketball_games = 0
#football_games = 0
Game.all.each do |game|
instance_variable_set("##{game.sport.name.downcase}_games", instance_variable_get("##{game.sport.name.downcase}_games") + 1)
end
Is there a better way to do this, than calling the get method inside the set method? I really am just trying to += the dynamic variable...
Building upon #Santosh's answer, you could do this more generally:
#games = Sport.all.map { |sport| [sport.name.to_sym, sport.games.count] }.to_h
Another solution, without the loop (Assuming the relation is sport has_many games)
#games = {
:baseball => Sport.find_by_name('baseball').games.count,
:basketball => Sport.find_by_name('basketball').games.count,
:football => Sport.find_by_name('football').games.count
}
#games = {:baseball => 0, :basketball => 0, :football => 0 }
Game.all.each do |game|
#games[game.sport.name.downcase.to_sym] = #games[game.sport.name.downcase.to_sym] + 1
end
Is there an example in Ruby using rally_api how to set State of a feature as mentioned here?
Specifically, is there a way to query the ObjectID or the fully qualified path of state to use
"State" => "Developing"
instead of
"State" => "/state/<ObjectID>"
It is possible to query State, create a hash, and populate the hash with the query results, where State Name is the key and State _ref is the value:
state_results.each do |s|
s.read
state_hash[s["Name"]] = s["_ref"]
end
Then we can update a State:
features.each do |f|
field_updates={"State" => state_hash["Developing"]}
f.update(field_updates)
end
Here is a code example:
#rally = RallyAPI::RallyRestJson.new(config)
queryState = RallyAPI::RallyQuery.new()
queryState.type = :state
queryState.fetch = "true"
queryState.workspace = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/workspace/11111" }
queryState.project = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/project/22222" } #use valid OIDs
queryState.query_string = "(TypeDef.Name = \"Feature\")"
state_hash = Hash.new
state_results = #rally.find(queryState)
state_results.each do |s|
s.read
#puts "Ref: #{s["_ref"]}, Name: #{s["Name"] }, TypeDef: #{s["TypeDef"]}"
state_hash[s["Name"]] = s["_ref"]
end
query = RallyAPI::RallyQuery.new()
query.type = "portfolioitem/feature"
query.fetch = "Name,FormattedID"
query.workspace = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/workspace/1111" }
query.project = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/project/22222" } #use valid OIDs
query.query_string = "(Name = \"my feature\")"
results = #rally.find(query)
features = [];
results.each do |f|
f.read
puts "Current state of Feature #{f["FormattedID"]}: #{f["State"].to_s}"
features << f
end
features.each do |f|
field_updates={"State" => state_hash["Developing"]}
f.update(field_updates)
puts "Feature #{f["FormattedID"]} is now in State: #{f["State"].to_s}"
end
i'm working on this script -> http://www.redmine.org/plugins/default_members <- by Sajin Andrei
I've completely modified the hook to suite my needs, but i realized that it actually adds every single user that is member of the defined group.
what i want to do is, instead, add the entire group to the project, so that if i add anyone else to the same group i'll not have to update every project.
this is my code:
# Debuggin [Default: commented/disabled]
#require 'logger'
class DefmembersHook < Redmine::Hook::ViewListener
def controller_projects_new_after_save(context={ })
#log = Logger.new('/usr/local/share/redmine/log/plugin.log')
params = context[:params]
project = context[:project]
roles = Role.find_all_givable
setting_group = Setting.plugin_redmine_default_members[:group] ? Setting.plugin_redmine_default_members[:group] : 'Manager'
groups ||= setting_group.split(",")
groups.each do |gp|
#log.error "gp: #{gp}"
group = Group.find(:first, :conditions => ["LOWER(lastname) = ?", gp.to_s.downcase])
#log.error "group: #{group}"
users = User.active.in_group(group).all(:limit => 100)
users.each do |user|
if user[:lastname] != 'Admin' && user[:lastname] != 'Anonymous'
#log.error "inizio per #{user}"
rs = Role.find_by_name(group.to_s)
#log.error "rs: #{rs}"
m = Member.new(:user => user, :roles => [rs])
project.members << m
#log.error "fine per #{user}"
end
end
end
end
end
and is actually working as intended by Sajin Andrei, adding single users from a group.
i want to do something like this
m = Member.new(:group => group, :roles => [rs])
project.members << m
but it doesn't work (obviously)...
hope someone can help
found the solution:
# Modificata da CARLO RUGGIERO per RETINA
# Permette di aggiungere interi gruppi ad un progetto in automatico!
class DefmembersHook < Redmine::Hook::ViewListener
def controller_projects_new_after_save(context={ })
params = context[:params]
project = context[:project]
#roles = Role.find_all_givable
setting_group = Setting.plugin_redmine_default_members[:group] ? Setting.plugin_redmine_default_members[:group] : 'Manager'
groups ||= setting_group.split(",")
groups.each do |gp|
group = Group.find(:first, :conditions => ["LOWER(lastname) = ?", gp.to_s.downcase])
rs = Role.find_by_name(group.to_s)
project.members << Member.new(:principal => group, :roles => [rs])
end
end
end
the problem was that the :user tag only accepts user elements
changing it to :principal i've got it to work
m = Member.new(:principal => group, :roles => [rs])
project.members << m
hope that someone find this usefull
I had got a task to select search the students those name start with param value and city in the selected value.How can i set in ruby on rails?
i did like this but this is not working
controller
def list
studentcount=Student.count()
puts studentcount
#studentname = Student.where("name name1 AND city = :cityId1",
{:name1 => params[:name], :cityId1 => params[:cityId]})
puts 'studentname'
puts #studentname.inspect
#students = Student.limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order(params[:jtSorting])
#jtable = {'Result' => 'OK','Records' => #students.map(&:attributes), :TotalRecordCount => studentcount}
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #jtable}
end
end
Try:
#studentname = Student.where("name LIKE :name1 AND city = :cityId1",
{:name1 => "#{params[:name]}%", :cityId1 => params[:cityId]})
This is a rather dirty solution, but pure ARel cannot handle this case the way you desire. You might want to try the Sqeel gem.
Try
#studentname = Student.where("name LIKE ? AND city = ?", "#{params[:name]}%", params[:cityId])
there is a error in your methods, it should be like this
#studentname = Student.where("name = :name1 AND city = :cityId1",
{:name1 => params[:name], :cityId1 => params[:cityId]})
With ruby on rails, I want to do something like:
#tasks = Task.where(:def => true || :house_id => current_user.house_id)
What is the most efficient/clean way to do this?
You can do it like this:
Task.where("def = ? or house_id = ?", true, current_user.house_id)
The general case is:
Model.where("column = ? or other_column = ?", value, other_value)
You can also leverage Arel:
t = Task.arel_table
#tasks = Task.where(
t[:def].eq(true).
or(t[:house_id].eq(current_user.house_id))
)