Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
=> [#<WelcomeCall id: 16, call_at: "2013-11-06 12:00:00">,
#<WelcomeCall id: 17, call_at: "2013-11-06 13:00:00">,
#<WelcomeCall id: 18, call_at: "2013-11-06 17:00:00">,
#<WelcomeCall id: 19, call_at: "2013-11-06 14:00:00">]
I would like to get an array of hours form all these WelcomeCall objects.
=> [12, 13, 17, 14]
WelcomeCall is rails model.
I used for that:
def self.scheduled_hours date
by_date(date).map do |wc|
wc.call_at.hour
end
end
but maybe there is better way?
You want to use map.
Try something like this:
new_array = old_array.map{ |welcome_call| welcome_call.call_at.strftime('%H').to_i }
Assuming that your welcome_call.call_at is a Date. If it's not a Date object, then try:
new_array = old_array.map do |welcome_call|
date = Date.parse(welcome_call.call_at)
date.strftime('%H').to_i
end
You could iterate over the array and parse the call_at datetime object using strftime('%H').to_i.
welcome_calls.map {|w| w.hour}
Parsing the Date to a String and then reparsing the String to a Fixnum is not as fast I think.
I just tried something and it worked, i'm not sure if it's the right way.
var = Model.all
t = var.collect(&:call_at)
hours_arr = []
t.each do |value|
hours_arr << value.time.hour
end
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to convert id into string to use in url
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id")
#t_id = #tutorial_id.to_s
render json: #t_id
Getting this error
S
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Tutorial Json array
{"demotutorials":[{"demotutorials":{"id":50}}]}
there are two things for your problem
if you using where the result is activerecord relation, which can
have couple of records, you must select which row, I gave you
sample first fow to choose first record
#tuturial_id is a activerecord row, so you must choose which column, I gave you sample using #tutorial_id.id
below is sample code:
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id").first
#t_id = #tutorial_id.id.to_s
render json: #t_id
Following code is work for me
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id").first
#t_id = #tutorial_id[0].id.to_s
render json: #t_id
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a method that I can to push to a JSON call. The function looks like:
def my_function(name, text , id_person, id_company)
end
What I want is to get:
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
Is there any easy way to do this?
The question is a bit unclear, but let me try with few answer possibilities
First of all this is not a JSON format
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
There should not be double quote (") before semicolon (:)
obj = {
my_function:
{
name: "names_value",
text: "text_value",
id_person: "id_person_value",
id_company: "id_company_value"
}
}
If you want to get each of the data in that "JSON" format, you could do this
name = obj['my_function']['name'] #name_value
text = obj['my_function']['text'] #text_value
and so on...
But if you want to make a string data into JSON format, you could do this
def my_function(name_value, text_value, id_person_value, id_company_value)
{
my_function:
{
name: name_value,
text: text_value,
id_person: id_person_value,
id_company: id_company_value
}
}.to_json
end
I hope it's answering your question...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm having problems when I'm trying to put a condition in params in my controller. This code is inside my controller:
if params[:example] == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
Is this code correct? How can I put a conditional in params controller?
You code looks okay. Unfortunately you do not say what problem you have. The only thing I see that may fail is the condition. If you pass some value into your params they are not typecasted. Therefore I guess you should use to_i in your condition:
if params[:example].to_i == 1
...
Try with
#table = Model.where((params[:example] == 1 ? 'column_table= ?' : 'column_table2 = ?'), params[:example]) unless params[:example].blank?
Here is code. You have save params[:example] in one variable.and use your coditions.
if params[:example].present?
#example = params[:example]
if #example == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
You can also send the column name along with its value and avoid ifs
#table = Model.find(:all,:conditions=>["#{params[:col]} = ?", params[:example] ] )
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
today I follow the rails guide and run a demo which include a scope as below:
scope :me, =>(keyword){where("title = ?",keyword)}
but it not work,so I change to :
scope :me, ->(keyword){where("title = ?",keyword)}
now it works,so I want to know the the difference between -> and => in rails
but I didn't find the result,so please tell me,thank you.
=> separates the keys from the values in a hashmap literal
-> - new lambda (syntactic sugar)
Examples :
h = { "foo" => "bar" }
l = ->{ "hello" }
l.call # => "hello"
The first is a syntax error. Wherever you read that, it's completely wrong.
The second is commonly known as 'stabby lambda syntax' - its a shortcut for writing:
lambda { |keyword| where('title = ?', keyword) }
More about lambdas in Ruby: http://rubymonk.com/learning/books/1-ruby-primer/chapters/34-lambdas-and-blocks-in-ruby/lessons/77-lambdas-in-ruby
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a hash e.g.
{{"badan"=>1, "bau"=>1, "China,"=>1, "pilot."=>1, "RT"=>1, "penentu"=>1, "merupakan"=>1, "China, test test bau"=>1, "satu"=>1, "merupakan salah salah satu"=>1, "RT #WOWFakta: #WOWFakta: Di"=>1, "Di"=>1, "seorang pilot."=>1, "kelulusan menjadi menjadi seorang"=>1, "seorang"=>1, "salah"=>1, "#WOWFakta:"=>1, "satu penentu penentu kelulusan"=>1, "Di Beijing Beijing China,"=>1, "menjadi"=>1, "kelulusan"=>1, "test"=>1, "bau badan badan merupakan"=>1, "Beijing"=>1}=>{"kebali"=>1, "kff"=>1, "box"=>1, "#zannahoctavia"=>1, "kebali kamu kamu ca?"=>1, "ca?"=>1, "kampus"=>1, "kantin"=>1, "kff hah hah goods"=>1, "#zannahoctavia haha haha kali"=>1, "aja"=>1, "box kemang kemang aja"=>1, "yaa"=>1, "china"=>1, "hah"=>1, "hah. Jadi Jadi kebali"=>1, "goods pw pw tapi"=>1, "aja di di kff"=>1, "di"=>1, "pw"=>1, "kemang"=>1, "haha"=>1, "kali kantin kantin kampus"=>1, "Jadi"=>1, "tapi"=>1, "tapi yaa yaa hah."=>1, "kampus china china box"=>1, "hah."=>1, "kamu"=>1, "kali"=>1}}
and I want to retrieve all keys' values in a very efficient way. How can I do this?
You can iterate your hash like that it contain hash within hash
h.each do |key, value|
puts key
value.values.each do |v|
puts v
end
end
your hash contains a key and a value that is also a hash. You can try the following
hash.flatten.map(&:keys).inject(:+)
or use flat_map
hash.flatten.flat_map(&:keys)