0

I have a serialized object :address in Hotel model and I don't know how to save it properly in the DB. I have the following:

#model hotel

class Hotel < ActiveRecord::Base
belongs_to :user
serialize :address, Hash
end

...and view 'new'

<%= form_for(@hotel) do |f| %>

  <%= f.label :title %>
  <%= f.text_field :title %>

  <%= f.label :stars %>
  <%= f.text_field :stars %>

  <%= f.label :room, "Room description" %>
  <%= f.text_area :room, size: "20x10" %>

  <%= f.label :price %>
  <%= f.number_field :price %>

  <%= f.fields_for :address do |o| %>     
    <%= o.label :country %>
    <%= o.text_field :country %>

    <%= o.label :state %>
    <%= o.text_field :state %>

    <%= o.label :city %>
    <%= o.text_field :city %>

    <%= o.label :street %>
    <%= o.text_field :street %>
  <% end %>

  <%= f.submit "Create hotel", class: "btn btn-large btn-primary" %>
<% end %>

With this code what I get is: hotel address nil...

Okay.. We will go another way. After googling much I came to this code:

# hotel.rb model

class Hotel < ActiveRecord::Base

  class Address
    include ActiveModel::Conversion
    extend ActiveModel::Naming

    attr_accessor :country, :state, :city, :street

    def persisted?; true end

    def id; 1 end

    def self.load json
      obj = self.new
      unless json.nil?
        attrs = JSON.parse json
        obj.country = attrs['country']
        obj.state = attrs['state']
        obj.city = attrs['city']
        obj.street = attrs['street']
      end
      obj
    end

    def self.dump obj
      obj.to_json if obj
    end  
  end

  belongs_to :user
  serialize :address, Address
end

and the same view new.html.erb

The result is: Address:0xb0e530c

So, nothing saves in the database... I don't know what to try next, I'll appreciate any help. Didn't know that serialized object will cause so much problems to me. THANKS!

PS Here's hotels_controller.

class HotelsController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]


  def new
    @hotel = Hotel.new
  end

  def index
    @hotels = Hotel.paginate(page: params[:page])
  end

  def show
    @hotel = Hotel.find(params[:id])
  end

  def create
    @hotel = current_user.hotels.build(hotel_params)    
    if @hotel.save      
      flash[:success] = "Hotel created!"
      redirect_to @hotel
    else
      render 'new'      
    end    
  end

  private

    def hotel_params
      params.require(:hotel).permit(:title, :stars, :room, :price, :address)
    end

end
Al17
  • 431
  • 7
  • 20
  • 1
    to answer your question, the first approach was right. Here's a clear answer with documentation. http://stackoverflow.com/a/21397522/1536309 - if you `puts params` inside the `def create` of the hotels controller you should be able to see all the params that are coming through. make sure you have address params. – Blair Anderson Mar 21 '16 at 05:16

1 Answers1

1

First thing on your migration file make sure that you are saving your fields as a test like

def self.up
   add_column : hotels, : address, :text
end

Then Rails will convert it into YAML / Hash for you (and perform proper serialization).

Wish you the best of luck.

PS take a look at https://stackoverflow.com/a/6702790/1380867

Community
  • 1
  • 1