سؤال

In my reservations table I have a rooms (text) field to store hash values such (1 => 3) where 1 is roomtype and 3 corresponds to the amount of rooms booked by the same agent.

My Reservation model

serialize reserved_rooms, Hash

Here is my nested resource

  resources :hotels do
    resources :roomtypes, :reservations
  end

RoomType stores a single room type which belongs to Hotel model. Though I can enlist roomtypes within my reservation form I do not know how I can create a dynamic hash via form to create/update this hash. I have this but I am looking for a way to create a dynamic hash "key, value" set. Meaning, if Hotel model has two RoomType my hash would be {12 = > 5, 15 => 1} (keys corresponds to the roomtype_ids while values are the amount}

<%= f.fields_for ([:roomtypes, @hotel]) do |ff| %>
    <% @hotel.roomtypes.each do |roomtype| %>
      <%= ff.label roomtype.name %> 
      <%= f.select :reserved_rooms, ((0..50).map {|i| [i,i] }), :include_blank => "" %>
    <% end %>
<% end %>

What I want is what this website has in the availability section (nr. of rooms):

specs: rails 4.1, ruby 2.1

Note: If you think there is a design problem with this approach (storing reserved_room in a serialized field) I can follow another path by creating another table to store the data.

هل كانت مفيدة؟

المحلول

Might need tweaking but i used similar code with check-boxes and it worked!

<% @hotel.roomtypes.each do |roomtype| %>
  <%= f.label roomtype.name %> 
  <%= f.select :"reserved_rooms[roomtype.id]", ((0..50).map {|i| [i,i] }), :include_blank => "" %>
<% end %>

نصائح أخرى

This gets messy enough that I would probably consider going with a separate models as you mentioned. I would simply do:

class Hotel < ActiveRecord::Base
  has_many :room_types      
  has_many :rooms, :through => :room_types
end

class RoomType < ActiveRecord::Base
  has_many :rooms
end

class Room < ActiveRecord::Base
  has_many :reservations
  belongs_to :room_type
end

class Reservation < ActiveRecord::Base
  belongs_to :room
  belongs_to :agent
end

class Agent < ActiveRecord::Base
  has_many :reservations
end

Then just use a generic form to submit the # Rooms integer, and let your controller handle making multiple reservations...? Maybe I'm not understanding your objective well enough...

Rails 4 has a new feature called Store you would love. You can easily use it to store a hash set which is not predefined. You can define an accessor for it and it is recommended you declare the database column used for the serialized store as a text, so there's plenty of room. The original example:

class User < ActiveRecord::Base
  store :settings, accessors: [ :color, :homepage ], coder: JSON
end

u = User.new(color: 'black', homepage: '37signals.com')
u.color                          # Accessor stored attribute
u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor

# There is no difference between strings and symbols for accessing custom attributes
u.settings[:country]  # => 'Denmark'
u.settings['country'] # => 'Denmark'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top