# Apn on Rails gem

**URL:** <https://www.ruby-forum.com/t/apn-on-rails-gem/191381>\
**Category:** Rails\
**Created:** [July 11, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381 "2010-07-11T14:13:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 11, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/1 "2010-07-11T14:13:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 11, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/2 "2010-07-11T18:03:46Z")

</div>

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.  
\*

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 11, 2010, 6:17pm UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/3 "2010-07-11T18:17:49Z")

</div>

Amit K. 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 …  
???

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 11, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/4 "2010-07-11T22:18:57Z")

</div>

token is your iphone id

On 11/07/2010, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > notification.device = device  
> > > > Thank you,  
> > > > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > > > To unsubscribe from this group, send email to  
> > > > removed\_email\_address@domain.invalid.  
> > > > For more options, visit this group at  
> > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

## –

Regards,  
Amit K.  
Ruby On Rails Consultant  
+91 9890654102  
Blogsite: [http://amitbaraik.blog.co.in/](http://amitbaraik.blog.co.in/)

This e-mail is confidential and may be legally privileged. If you are  
not the intended recipient, please notify the sender immediately and  
delete this e-mail.

???

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 12, 2010, 8:20am UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/5 "2010-07-12T08:20:00Z")

</div>

## I mean device.id represents your token from apn\_devices table.

Regards,  
Amit K.  
Ruby On Rails Consultant  
+91 9890654102  
Blogsite: [http://amitbaraik.blog.co.in/](http://amitbaraik.blog.co.in/)

This e-mail is confidential and may be legally privileged. If you are  
not  
the intended recipient, please notify the sender immediately and delete  
this  
e-mail.

???

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 12, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/6 "2010-07-12T19:47:42Z")

</div>

Amit K. wrote:

> I mean device.id represents your token from apn\_devices table.

I don’t understand… I would it look like in production???

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 30, 2010, 8:23am UTC](https://www.ruby-forum.com/t/apn-on-rails-gem/191381/7 "2010-07-30T08:23:11Z")

</div>

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.

On Jul 12, 10:47 am, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Amit K. 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
