· 8 years ago · Jun 03, 2018, 03:38 PM
1class User < ActiveRecord::Base
2 has_many :posts
3end
4
5
6class Post < ActiveRecord::Base
7 belongs_to :user
8end
9
10
11--
12-- Table structure for table `posts`
13--
14
15CREATE TABLE IF NOT EXISTS `posts` (
16 `id` int(11) unsigned NOT NULL auto_increment,
17 `user_id` int(11) default NULL,
18 `title` varchar(100) NOT NULL,
19 `slug` varchar(100) NOT NULL,
20 `content` text NOT NULL,
21 `publish_status` enum('publish','draft','review') NOT NULL,
22 `created` datetime NOT NULL,
23 `modified` datetime NOT NULL,
24 PRIMARY KEY (`id`)
25) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Posts' ;
26
27-- --------------------------------------------------------
28
29--
30-- Table structure for table `users`
31--
32
33CREATE TABLE IF NOT EXISTS `users` (
34 `id` int(11) unsigned NOT NULL auto_increment,
35 `username` varchar(25) NOT NULL,
36 `password` varchar(100) NOT NULL,
37 `full name` varchar(100) NOT NULL,
38 `birthdate` date NOT NULL,
39 `created` datetime NOT NULL,
40 `modified` datetime NOT NULL,
41 PRIMARY KEY (`id`)
42) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='users table' ;
43
44In Ruby Script Console:
45[root@adsl-126]# script/console
46Loading development environment (Rails 2.2.2)
47>> Post.find :first
48=> #<Post id: 1, user_id: 1, title: "Welcome to my site!", slug: "welcome-to-my-site", content: "Welcome to my site!", publish_status: "publish", created: "2009-02-09 09:14:12", modified: "2009-02-09 09:14:12">
49>>