Note that there are some explanatory texts on larger screens.

plurals
  1. POAn ActiveRecord has its `id` == nil after saving
    primarykey
    data
    text
    <p>It is really awkward that the <code>id</code> of an ActiveRecord is still <code>nil</code> after <code>save</code>.</p> <p>My command in console is as follows:</p> <pre><code>irb(main):003:0&gt; c = Comment.new =&gt; #&lt;Comment id: nil, commentable_id: nil, commentable_type: nil, user_id: nil, content: "", del_flg: 0, created_at: nil&gt; irb(main):004:0&gt; c.commentable = Moment.last Moment Load (0.2ms) SELECT `moments`.* FROM `moments` ORDER BY `moments`.`id` DESC LIMIT 1 =&gt; #&lt;Moment id: 119583... irb(main):005:0&gt; c.user = User.find(119) User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 119 LIMIT 1 =&gt; #&lt;User id: 119... irb(main):006:0&gt; c.save (0.2ms) BEGIN SQL (0.6ms) INSERT INTO `comments` (`commentable_id`, `commentable_type`, `content`, `created_at`, `del_flg`, `id`, `user_id`) VALUES (119583, 'Moment', '', '2013-01-22 15:19:05', 0, NULL, 119) (0.1ms) COMMIT =&gt; true irb(main):007:0&gt; c =&gt; #&lt;Comment id: nil, commentable_id: 119583, commentable_type: "Moment", user_id: 119, content: "", del_flg: 0, created_at: "2013-01-22 15:19:05"&gt; irb(main):008:0&gt; c.id =&gt; nil </code></pre> <p>This is how I defined my record:</p> <pre><code>class Comment &lt; ActiveRecord::Base belongs_to :user belongs_to :commentable, polymorphic: true end </code></pre> <p>At first, I thought it was that <code>id</code> is not set to the <code>PRIMARY KEY</code> in my MySQL database, so I checked it:</p> <pre><code> mysql&gt; describe comments; +------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | commentable_id | int(11) | NO | MUL | NULL | | | commentable_type | varchar(100) | NO | MUL | NULL | | | user_id | int(11) | NO | | NULL | | | content | text | NO | | NULL | | | del_flg | tinyint(4) | NO | | 0 | | | created_at | datetime | NO | | NULL | | +------------------+--------------+------+-----+---------+----------------+ </code></pre> <p>Anyone has any idea about how this would come? Thanks in advance.</p> <p><strong>EDIT</strong>:</p> <p>Just realize that the SQL explanation for <code>c.save</code> is <code>INSERT INTO <code>comments</code> (<code>commentable_id</code>, <code>commentable_type</code>, <code>content</code>, <code>created_at</code>, <code>del_flg</code>, <code>id</code>, <code>user_id</code>) VALUES (119583, 'Moment', '', '2013-01-22 15:19:05', 0, NULL, 119)</code></p> <p>Well, this record is in fact inserted into database.</p> <pre><code>mysql&gt; select * from comments where commentable_id = 119583 and commentable_type = "Moment" -&gt; ; +-------+----------------+------------------+---------+---------+---------+--------- ------------+ | id | commentable_id | commentable_type | user_id | content | del_flg | created_at | +-------+----------------+------------------+---------+---------+---------+---------------------+ | 26582 | 119583 | Moment | 119 | | 0 | 2013-01-22 14:58:22 | | 26583 | 119583 | Moment | 119 | | 0 | 2013-01-22 15:09:11 | | 26585 | 119583 | Moment | 119 | | 0 | 2013-01-22 15:19:05 | | 26586 | 119583 | Moment | 119 | | 0 | 2013-01-22 15:38:14 | +-------+----------------+------------------+---------+---------+---------+---------------------+ </code></pre> <p>More tests:</p> <pre><code>irb(main):009:0&gt; c.persisted? =&gt; true irb(main):010:0&gt; c.errors =&gt; #&lt;ActiveModel::Errors:0x00000004761958 @base=#&lt;Comment id: nil, commentable_id: 119583, commentable_type: "Moment", user_id: 119, content: "", del_flg: 0, created_at: "2013-01-22 16:52:59"&gt;, @messages={}&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload