accessors and update_attributes

I have a model with a location attribute, it's a array of two elements; latitude and longitude. I define the accessors for the location like this

class Address
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Spacial::Document

    field :location,        :type => Array,    spacial: {lat: :latitude, lng: :longitude, return_array: true }


    #accessors for location


    def latitude
        location[0]
    end

    def latitude=( lat )
        location[0] = latitude
    end

    def longitude
        location[1]
    end

    def longitude=( lng )
        location[1] = lng
    end
    attr_accessible :location, :latitude, :longitude

end

here is the controller code

def create
        @address = Address.new(params[:address])
        if @address.save
            redirect_to :action => 'index'
        else
            render :action => 'new'
        end
    end

    def update
        @address = Address.find(params[:id])

        if @address.update_attributes(params[:address])
            redirect_to :action => 'index'
        else
            render :action => 'edit'
        end

    end

and at the view level

<%= f.hidden_field :latitude%>
<%= f.hidden_field :longitude%>

these hidden field are manipulated via js, and that is ok. I saw it view developers tools

Here are the parameters the controller receives

"address"=>{"latitude"=>"-38.0112418", "longitude"=>"-57.53713060000001", "city_id"=>"504caba825ef893715000001", "street"=>"alte. brown", "number"=>"1234", "phone"=>"223 4568965"}, "commit"=>"Guardar", "id"=>"504cacc825ef893715000006"}

Note that the latitude and longitude parameters changed and thats ok, but this change it's not saved to the mongodb

So, The values for latitude and longitude are not saved. Is there any instruction my code is missing?

Thanks in advance.

--- edit ---

here are the working accessors

    def latitude
        location[0]
    end

    def latitude=( lat )
        self.location = [lat,self.location[1]]
    end

    def longitude
        location[1]
    end

    def longitude=( lng )
        self.location = [self.location[0], lng]
    end

Answers


When you want to set a field of your database, use self always safer and it's a good habit.

Second thing, you have to use the argument you pass to the setter.

Resulting code:

def latitude
  location[0]
end

def latitude=( lat )
  self.location[0] = lat
end

def longitude
  location[1]
end

def longitude=( lng )
  self.location[1] = lng
end

Need Your Help

Catch exception by class?

java exception

I would like to catch an exception I expect but allow the others through.

Mathematical operations on MySQL DECIMAL data type in PHP

php mysql math decimal sqldatatypes

I am storing money values on a MySQL table and I'm using DECIMAL as the data type. I need to grab these elements, do some basic mathematical operations on these values (addition, subtraction, sums,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.