top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create an OrderedHash in Rails?

+1 vote
287 views

I'm a little confused about how one is supposed to create an ordered hash.

As far as I can tell, ActiveSupport::OrderedHash does not provide a constructor or a method that can create a hash from an array of pairs. Is this a deliberate ommission?

Hash also doesn't provide this, although later versions of ruby provide .to_h on an array as a way of generating a hash.

Why isn't there an OrderedHash.from_a( method? Why not, perhaps, have ActiveSupport patch Array to provide a ".to_ordered_h" method on an array? If there's an obvious way to create an ordered hash, please let me know about it. FWIW, my particular application was converting an ordered CsvRow to an ordered hash, which seem to involve my code than I felt should be necessary.

posted Sep 1, 2014 by Sumit Pokharna

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes

I'm trying to Add Categories to my Rails app, but don't quite know how to do this.

I have many Pins(Images) and want the user to be able to assign a category on those Pins. ASSIGN not create, edit, or delete a Category, just selecting one for their Pin. Meaning that, When a user uploads a
pin, he can choose from a dropdown list a Category.

Then, another user can choose from the Menu a Category, and ONLY the Pins in this Category will be listed. How can i do this? Any pointer??

+2 votes

I am trying to write a simple calendar app and having troubles with time zones. For as many nice Date and Time helpers, action view and active record sure dont play nice with time zones.

I have an appointment model with the appointment_time attribute which is a datetime. My current issue is editing appointment_time with datetime_select within a form tag.

def appt_time
appointment_time.in_time_zone(time_zone) #this is the appointment owners time zone 
end

This gives a select with the current date and time in UTC, not the appointment date and time. appt_time does get called and returns the correct date and time adjusted for the time zone.

The docs mention nothing about time zones, what is the best way to accomplish this?

+2 votes

I am trying to develop an app to manage media files I have a folder with pictures, audio and video files.

I am using Dir.glob to list the directory in my controller and in my views I am using match with regular expressions to test if the file ends in mp3 jpg mp4 3gp wav etc ... the code I wrote seems to almost work The variable @file is passed on from the controller and here is my view.

Is this a sensible way of trying to do this?

<% if @file.match("mp4") %>
 <video width="320" height="240" controls>
  <source src= <%= @file %> type="video/mp4">
 </video> 
<% elsif @file.match("mp3") %>
 <audio controls preload="auto">
  <source src = <%= @file %> >
 </audio>
<% elsif @file.match("jpg") %>
 <img src= "<%= @file %>">
<% end %>
+2 votes

I am working on building an api client for the Rakuten Market-place. I have got some test requests for add delete update etc working and thought I should aim to structure it as a Gem and publish it so that others can use it/enhance it.

I haven't built a Gem before (worked mostly within the rails environment to date).

Have been reading and looking at other api client gems and am making progress on building something (still got a way to go to handle errors etc.)

To make the gem general purpose though, I am trying to figure out the best way to provide the mapping between models in a rails app and the api client objects (such as product, category, order etc).

I am aiming to make each client api object a class be (or should i call them models?)
Then I suspect I will use new to build an api instance from a rails instance, and find to return an api instance to a rails instance.

I can build in mapping for my own models to the api objects with no problem, but I can't see how I could generalize this so that other apps with similar models could use the gem.

I suspect there are approaches for doing this, but so far I haven't managed to come up with how to do it. If anyone has any wisdom on the matter I would be grateful.

+1 vote

Example
Map data = new HashMap();
data.put("a","America");
data.put("a","Africa");
data.put("b","Bangladesh");

Need to have both "America" and "Africa" as value against key "a". Is there any mechanism to achieve this scenario.

...