J Bourne
J Bourne

Reputation: 1439

rails: datetimes fields do not fetch the values

I've written a code with acts_as_api for a dates_model. The code is as following. It never fetches the dates. Am I doing anything wrong. The code is as follows

    acts_as_api

    api_accessible :bill_corrections do |inv|
       inv.add :common_date, :as => :recieved_date, :if => lambda{|u|u.date_type_code=="RECVD"}
       inv.add :common_date, :as => :actual_date, :if => lambda{|u|u.date_type_code=="ARRIV"}
  end

I've my schema for datefield

create_table "item_dates", :force => true do |t|
    t.integer  "item_date_id",   :limit => 10
    t.string   "date_type_code"
    t.datetime "common_date"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Upvotes: 0

Views: 203

Answers (1)

chris_b
chris_b

Reputation: 1294

I'm the creator of acts_as_api. I tried your use case with a minimal Rails app and it worked for me. Here is an example response in xml:

<users type="array">
  <user>
    <first-name>Me</first-name>
    <recieved-date type="datetime">2011-10-07T08:20:02Z</recieved-date>
  </user>
  <user>
    <first-name>Me2</first-name>
    <actual-date type="datetime">2011-10-07T08:20:52Z</actual-date>
  </user>
</users>

The api template in the model looks like this:

api_accessible :test_dates do |t|
  t.add :first_name
  t.add :common_date, :as => :recieved_date, :if => lambda{|u|u.date_type_code=="RECVD"}
  t.add :common_date, :as => :actual_date, :if => lambda{|u|u.date_type_code=="ARRIV"}
end

And this is a dump of User.all:

[
    [0] #<User:0x0000010b859308> {
                    :id => 1,
            :first_name => "Me",
             :last_name => "Too",
                   :age => nil,
                :active => nil,
            :created_at => Fri, 07 Oct 2011 08:20:19 UTC +00:00,
            :updated_at => Fri, 07 Oct 2011 08:20:19 UTC +00:00,
        :date_type_code => "RECVD",
           :common_date => Fri, 07 Oct 2011 08:20:02 UTC +00:00
    },
    [1] #<User:0x0000010b858688> {
                    :id => 2,
            :first_name => "Me2",
             :last_name => "Too2",
                   :age => nil,
                :active => nil,
            :created_at => Fri, 07 Oct 2011 08:20:45 UTC +00:00,
            :updated_at => Fri, 07 Oct 2011 08:20:53 UTC +00:00,
        :date_type_code => "ARRIV",
           :common_date => Fri, 07 Oct 2011 08:20:52 UTC +00:00
    }
]

Did you check that you use the controller helpers correctly?

def index
  @users = User.all

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render_for_api :test_dates, :xml => @users, :root => :users  }
    format.json { render_for_api :test_dates, :json => @users, :root => :users }
  end
end

I hope this helps. :)

Further debugging could include: * What happens if you remove the :if options? * What does 'puts u.date_type_code' say if you put it in the :if options blocks?

Upvotes: 1

Related Questions