Apn on Rails gem

Hi, I'm really not sure someone will be able to help me, but I have a problem with apn_on_rails. When I follow the exemple on the github page I have an error:

device = APN::Device.create(:token => "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX")

  >> notification = APN::Notification.new   >> notification.device = device   >> notification.badge = 5   >> notification.sound = true   >> notification.alert = "foobar"   >> notification.save

My error: activeRecord::StatementInvalid: Mysql::Error: Column 'device_id' cannot be null: INSERT INTO `apn_notifications` (`custom_properties`, `created_at`, `device_language`, `updated_at`, `device_id`, `sent_at`, `badge`, `errors_nb`, `sound`, `alert`) VALUES(NULL, '2010-07-11 11:22:36', NULL, '2010-07-11 11:22:36', NULL, NULL, 5, 0, 1, 'foobar')

My question is where to I set the device_id ? I can't do: device.device_id = XXXXXXXX

Greg

Hi, Try this, i did it same way

device = APN::Device.create(:token => “XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX”) notification = APN::Notification.new device.id= ‘xxxxxxx’ notification.device = device notification.badge = 5 notification.sound = true notification.alert = “foobar” notification.save I tried the same way in my console… See APN: Notifications table and the device id will be in APN: Device table.

Amit Kumar wrote:

Hi,

Try this, i did it same way

device = APN::Device.create(:token => "XXXXXXXX XXXXXXXX XXXXXXXX

XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX") >> notification = APN::Notification.new

device.id= 'xxxxxxx'

>> notification.device = device >> notification.badge = 5 >> notification.sound = true >> notification.alert = "foobar" >> notification.save    * I tried the same way in my console.. See APN: Notifications table and the device id will be in APN: Device table. *

Thank you, The device id is the iphone ID ? or the token is the iphone ID? When I save the notification, it device isnt storein the database ... ???

token is your iphone id

I mean device.id represents your token from apn_devices table.

Amit Kumar wrote:

I mean device.id represents your token from apn_devices table.

I don't understand... I would it look like in production???

Greg

Hello,

I know this issues is a couple of weeks old, but was still one of the more recent hits I found when looking for discussions of apn_on_rails.

Amit Kumar wrote: > I mean device.id represents your token from apn_devices table.

This is really an ActiveRecord issue ... device.id is the database identifier of the record, not the APN token (which is "token").

In the code above, instead of assigning the to token to "device.id", assign it to "device.token". Then save the record: "device.save", and if you get back 'true', you can procede with the rest of your code. As id is an int, you couldn't put a string in there, which is what the token is. You'd get an error when trying to save, and the token field wouldn't be populated.

Just remember, unless you're stepping off the 'golden path' (and only do so when you need to and know what you're doing), with ActiveRecord, the 'id' field does not have any business data in it, it is just there to identify the record itself, in most cases, automatically assigned (possibly not with Oracle, but that's another story). It can be useful to assign without having to actually retrieve a record, e.g.,

class User < ActiveRecord::Base   belongs_to :office end

Then, if you know the LA office is #3 e.g., you can do

  someuser.office_id = 3   someuser.save

instead of

someuser.office = Office.find_by_name("LA") someuser.save

which will have to fetch another record, if Rails hasn't cached it already. It is frequent in controllers you'll have a record number rather than a record, coming back in the params from a form.

But in many cases, you can pretty much ignore the ID field.

Good luck, Craig