Ruby sort an array in ascending order [closed] - ruby-on-rails

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 an array ['f_1', 'f_10', 'f_3', 'f_2']
I want to sort this array in ascending order using ruby one line code.

Do as below using sort_by :
['f_1', 'f_10', 'f_3', 'f_2'].sort_by { |s| s[/\d+/].to_i }
# => ["f_1", "f_2", "f_3", "f_10"]

Related

Ruby and Rail - How to combine date and time to get data [closed]

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 last month.
Improve this question
Currently, I have 3 columns : booking_date, booking_time_from, booking_time_to.
Problem is how can i get all data have booking time 2 hours ago using Active Support ?
Booking.where([booking_time], 2.hours.ago.to_datetime)
I'm not sure about what's the difference between booking_time_from and booking_time_to, and which column that the booking time you're referring here. Let's assume it's booking_time_to, you can
Booking.where("booking_time_to < ?", 2.hours.ago)

Ruby/Rails update model attribute with array items? [closed]

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 am trying to populate column_name with items from #enc. Problem is #update_all updates all fields with last item in array?
Last item is "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n".
I am trying to populate with all items from array not just last.
I hope question is clear?
I tried #update_attributes, but no success?
Help.
Thanks
#enc=["hUt7ocoih//kFpgEizBowBAdxqqbGV1jkKVipVJwJnPGoPtTN16ZAJvW9tsi\n3inn\n", "wGNyaoEZ09jSg+/IclWFGAXzwz5lXLxJTUKqCFIiOy3ZXRgdwFUsNf/75R2V\nZm83\n", "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n"]
#enc.each do |i|
PaymentMethod.update_all(enc_number: i)
end
PaymentMethod.all.each_with_index do |payment, n = 0|
payment.update_column(:enc_number, #enc[n])
n +=1
end

Rails 4 rewrite of join and group query [closed]

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
Here is my old query:
Dispenser.includes(:dispedus).includes(:educations).group('education.name')
To reiterate my post title please rewrite this to enable the same query in Rails 4.
Put in question form, how can this be rewritten to work in rails 4 ? ? ?
With multiple independent associations use commas:
Dispenser.joins(:dispedus, :educations).group('educations.name')
http://guides.rubyonrails.org/active_record_querying.html#joining-multiple-associations
With a nested join you can write:
Dispenser.joins(dispedus: :educations).group('educations.name')
http://guides.rubyonrails.org/active_record_querying.html#joining-nested-associations-single-level

Dynamically Define Variables [closed]

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 9 years ago.
Improve this question
arrayName = #"Blah";
NSArray (array(arrayName))= [something or other];
After defining a variable name called "arrayName", which I wish to the name of an array to be it's condition. Is the code defining the NSArray possible?
P.S. The code is pseudo code.
sounds like you want to use an NSMutableDictionary then you can have your variable names as the keys, and assign them their specific values.
but what you are asking isnt possible

How to insert into a value an HTML insert [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to do a simple login form that just compare the value that you insert into the input in HTML to the data you had in the database. How can I do it?
Setup a controller and compare the params hash.
expected_value = '...'
if params[:form_value] == expected_value
do_something()
else
do_something_else()
end

Resources