{"id":1131,"date":"2015-08-05T03:23:10","date_gmt":"2015-08-05T03:23:10","guid":{"rendered":"http:\/\/www.honobono-life.info\/wpeng\/?p=1131"},"modified":"2015-08-05T03:23:10","modified_gmt":"2015-08-05T03:23:10","slug":"micropost-modelassociations","status":"publish","type":"post","link":"http:\/\/www.honobono-life.info\/wpeng\/micropost-modelassociations\/","title":{"rendered":"Ruby on Rails Tutorial micropost model,associations"},"content":{"rendered":"<p><strong>(1)micropost model<\/strong><\/p>\r\n\r\n<p><strong>1.basic model<\/strong><\/p>\r\n\r\n<p>$ rails generate model Micropost content:string user_id:integer<\/p>\r\n\r\n<p><strong>2.add index<\/strong><\/p>\r\n\r\n<p>$ vi db\/migrate\/&#8230;._create_microposts.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nclass CreateMicroposts &lt; ActiveRecord::Migration\r\n  def change\r\n    create_table :microposts do |t|\r\n      t.string :content\r\n      t.integer :user_id\r\n\r\n      t.timestamps\r\n    end\r\n    add_index :microposts, [:user_id, :created_at]\r\n  end\r\nend<\/pre>\r\n\r\n<p><strong>3.migrate<\/strong><\/p>\r\n\r\n<p>$ bundle exec rake db:migrate<\/p>\r\n\r\n<p>4.micropost validations<\/p>\r\n\r\n<p>$ vi app\/models\/micropost.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nclass Micropost &lt; ActiveRecord::Base\r\n  validates :content, presence: true, length: { maximum: 140 }\r\n  validates :user_id, presence: true\r\nend<\/pre>\r\n\r\n<p><strong>(2)User\/Micropost associations<\/strong><\/p>\r\n\r\n<p><strong>1. Micropost model<\/strong><\/p>\r\n\r\n<p>$ vi app\/models\/micropost.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nclass Micropost &lt; ActiveRecord::Base\r\n  belongs_to :user\r\n......<\/pre>\r\n\r\n<p><strong>2. User model<\/strong><\/p>\r\n\r\n<p>$ vi app\/models\/user.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nclass User &lt; ActiveRecord::Base\r\n  has_many :microposts, dependent: :destroy\r\n.....<\/pre>\r\n\r\n<p><strong>3. check<\/strong><\/p>\r\n\r\n<p>$ rails console<\/p>\r\n\r\n<p>2.0.0p247 :001 &gt; micropost = Micropost.create(content: &quot;Test #1&quot;, user_id: 10)<\/p>\r\n\r\n<p>2.0.0p247 :002 &gt; micropost = Micropost.create(content: &quot;Test #2&quot;, user_id: 10)<\/p>\r\n\r\n<p>2.0.0p247 :003 &gt; micropost=Micropost.find_by(user_id: 10)<br \/>\r\n&nbsp; Micropost Load (0.3ms) &nbsp;SELECT &nbsp;&quot;microposts&quot;.* FROM &quot;microposts&quot; &nbsp;WHERE &quot;microposts&quot;.&quot;user_id&quot; = 10 LIMIT 1<br \/>\r\n&nbsp;=&gt; #&lt;Micropost id: 1, content: &quot;Test #1&quot;, user_id: 10, created_at: &quot;2015-07-21 00:00:50&quot;, updated_at: &quot;2015-07-21 00:00:50&quot;&gt;<\/p>\r\n\r\n<p>2.0.0p247 :004 &gt; micropost.user<br \/>\r\n&nbsp; User Load (0.1ms) &nbsp;SELECT &nbsp;&quot;users&quot;.* FROM &quot;users&quot; &nbsp;WHERE &quot;users&quot;.&quot;id&quot; = ? LIMIT 1 &nbsp;[[&quot;id&quot;, 10]]<br \/>\r\n&nbsp;=&gt; #&lt;User id: 10, name: &quot;testuser10&quot;, email: &quot;test10@example.com&quot;, created_at: &quot;2015-07-13 08:45:35&quot;, updated_at: &quot;2015-07-20 08:30:47&quot;, password_digest: &quot;$2a$10$BLM\/nUrE1wptItRDOkKm4OynNZ718yjoDnnIBUkWL0s&#8230;&quot;, remember_token: &quot;48c3edd244e42156c38e89fca5fe5616c5cfcce1&quot;, admin: false&gt;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>(1)micropost model 1.basic model $ rails generate model Micropost content:string user_id:integer 2.add index $ vi db\/migrate\/&#8230;._create_microposts.rb class CreateMicroposts &lt; ActiveRecord::Migration def change create_table :microposts do |t| t.string :content t.integer :user_id t.timestamps end add_index :microposts, [:user_id, :created_at] end end 3.migrate $ bundle exec rake db:migrate 4.micropost validations $ vi app\/models\/micropost.rb class Micropost &lt; ActiveRecord::Base validates :content, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[180,178,179],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1131"}],"collection":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/comments?post=1131"}],"version-history":[{"count":1,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1132,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1131\/revisions\/1132"}],"wp:attachment":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/media?parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/categories?post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/tags?post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}