3

I'm currently using an API and not a database, and I want to be as close as ActiveRecord, so I decided to go ahead and do exactly like this railscast here: http://railscasts.com/episodes/219-active-model

So far, my save method works well, so I can save data to the API. My problem is with the edit, my find method seems to be the problem... Here is some code!

Edit method in my controller

  def edit
    @parking = Parking.find(params[:id])
  end

Whole model

class Parking
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend  ActiveModel::Naming

  attr_accessor :name, :address, :city, :longitude, :latitude, :contributor_name, :contributor_email

  validates_presence_of :name, :address, :city, :longitude, :latitude

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def self.find(id)
    parking           = Parse.get("Parking", id.to_s) // API Call

    name              = parking["name"]
    address           = parking["address"]
    city              = parking["city"]
    longitude         = parking["location"]["longitude"]
    latitude          = parking["location"]["latitude"]
    contributor_name  = parking["contributorName"]
    contributor_email = parking["contributorEmail"]

    return self
  end

  def save
    if (!valid?)
      return false
    else
      parking = Parse::Object.new("Parking")

      data =
      {
        :longitude => 40.0,
        :latitude  => -30.0
      }

      point = Parse::GeoPoint.new(data)

      parking["location"]         = point
      parking["name"]             = name
      parking["address"]          = address
      parking["city"]             = city
      parking["contributorName"]  = contributor_name
      parking["contributorEmail"] = contributor_email

      if (parking.save)
        return true
      end
    end
  end

  def persisted?
    false
  end
end

Here is the error I currently get:

undefined method `to_key' for Parking:Class

Extracted source (around line #1):

1: <%= form_for(@parking, :html => { :class => "form-horizontal"}) do |f| %>
2:   <% if @parking.errors.any? %>
3:     <div class="alert alert-error fade in">
4:       <a class="close" data-dismiss="alert">×</a>**

If anybody as suggestions, I'm open to any ideas really, I'm beginning with rails :)

Thanks!

EDIT:

When I do in my controller edit method something like:

  def edit
    @parking = Parking.new
    @parking.name              = "foo"
    @parking.address           = "foo"
    @parking.city              = "foo"
    @parking.longitude         = "foo"
    @parking.latitude          = "foo"   
  end

My view load foo in every fields no problemo, so the problem is I must be doing something wrong with the find method :)

allaire
  • 5,995
  • 3
  • 41
  • 56

1 Answers1

4

One problem is that your find method is a class method (by virtue of being 'self.find'), meaning it does not operate on an instance of the class and therefore has no knowledge of instance variables/methods such as name, address etc.

A better way to implement find is to instantiate a new instance of Parking, and populate it's variables, then return it e.g.

def self.find(id)
   raw = Parse.get("Parking", id.to_s)

   parking = Parking.new
   parking.name = raw["name"]
   #  etc for the others

   parking # Return the newly-created instance
end

This doesn't explain the 'undefined method' you're currently seeing, you may need to post up more detail to get an answer for that, particularly a full backtrace from the exception to see which bit of code is actually raising it. From the information you've supplied I'd guess that something within the Parse.get method is causing it.

Jon M
  • 11,669
  • 3
  • 41
  • 47
  • Oh great, that did work! Before going to bed yesterday, I did something similar, but I was doing the Parking.new in the controller and not in my class method (which I didn't like because it's not like ActiveRecord). Just before I accept this as the answer, can you tell me if it's a common practice to instantiate the current class in one of it's own class method like self.find? Is this how the find method in ActiveRecord works? – allaire Mar 05 '12 at 14:34
  • 2
    I can't say how ActiveRecord does it exactly, had a brief look but there are many layers of abstraction in there. It is common for methods which operate on the collection (e.g. create, find etc) to be a class method of the Parking class, and use instance methods for save, update etc. – Jon M Mar 05 '12 at 15:05