Example Of has_many In Rails's ActiveRecord

原创 2007年09月21日 16:44:00

一个简单的ActiveRecord中的has_many的示例,数据库中的表的建立SQL语句为:

create table items (
id int not null auto_increment,
title varchar(50) not null,
primary key (id)
);

create table users (
id int not null auto_increment,
name varchar(50) not null,
primary key (id)
);

create table items_users (
item_id int not null,
user_id int not null,
read_time date,
constraint fk_cp_item foreign key (item_id) references items(id),
constraint fk_cp_user foreign key (user_id) references users(id),
primary key (item_id, user_id)
);

 

执行后结果为:

#"Thinking in JAVA", "id"=>"1"}>
#"Tom", "id"=>"1"}>
----------------------------------------
[#"Thinking in JAVA", "id"=>"1"}>]
[#"Tom", "id"=>"1"}>]
----------------------------------------
2007-06-20
2007-06-20

require "rubygems"
require 
"active_record"
require 
"pp"

ActiveRecord::Base.establish_connection(
    :adapter 
=> "mysql",
    :host 
=> "localhost",
    :database 
=> "book2",
    :username 
=> "root",
    :password 
=> "",
    :port 
=> 3306
)

class Item < ActiveRecord::Base
    has_many :items_users, :dependent 
=> :destroy
    has_many :users, :through 
=> :items_users
end


Item.create([
    {:title 
=> "Thinking in JAVA"},
    {:title 
=> "Ruby on Rails Routing"},
    {:title 
=> "Agible Development with Rails"}
])


class ItemsUser < ActiveRecord::Base
    belongs_to :item
    belongs_to :user
end

class User < ActiveRecord::Base
    has_many :items_users, :dependent 
=> :destroy
    has_many :items, :through 
=> :items_users
    
    
def read_item(item)
        ItemsUser.create(:item 
=> item,:user => self,:read_time => Date.today)
    end
end


User.create([
    {:name 
=> "Tom"},
    {:name 
=> "Mary"},
    {:name 
=> "Jane"}
])


pp item 
= Item.find(:first)
pp user 
= User.find(:first)
puts 
"----------------------------------------"
user.read_item(item)
pp user.items
pp item.users
puts 
"----------------------------------------"
puts user.items_users.find(:first).read_time
puts item.items_users.find(:first).read_time

 

 

相关文章推荐

【Rails】inverse_of在has_many和belongs_to中的用法

最近使用Rails时,遇到了通过关联关系来多次访问同一条record,虽然得到的内容一样,但是每次都会创建不同对象的情况。通过查询Rails官方文档,找到了对关联关系的一种设置inverse_of,可...

SDK更新后,重新打开Eclipse时提示错误Location of the Android SDK has not been setup in the preferences

SDK更新后,重新打开Eclipse时提示错误Location of the Android SDK has not been setup in the preferences 现象描述       ...
  • fpxty
  • fpxty
  • 2017年05月12日 18:00
  • 863

how many does the factorial of n(in binary data ) have zero?

how many does the factorial of n(in binary data ) have zero? 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:g...

location of the android sdk has not been setup in the preferences

打开eclipse时出现无法识别android的项目,即所有android项目均显示红色错误标记。点击eclipse管理android SDK and AVD Manager 插件的标志时出现错误: ...

location of the android sdk has not been setup in the preferences的解决方法

本文转自:http://hi.baidu.com/azhangdeng/blog/item/063b4e90109ec5176e068c8d.html location of the a...
  • a7a9a68
  • a7a9a68
  • 2012年06月06日 09:56
  • 8740

ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQ L DATA in its decla

ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declar...

How many files in the "res" of the famous IM app.

对于Auto Test Engineer来说,values folder 里面的内容是最有用的了。 res │ ├─anim │ activity_alpha_action.x...
  • shinpox
  • shinpox
  • 2014年05月05日 00:00
  • 540

个人重启Eclipse时报Location of the Android SDK has not been setup in the preferences的解决方法

在重启Eclipse时报Location of the Android SDK has not been setup in the preferences的解决方法...

When many files was in deep directory of eMMC, Nozomi crash when connected to PC

78% INFO 02-18 13:02:28.583 (135:135) DEBUG Opening /data/log/debuggerd.log 78% INFO 02-18 13:02:28...

两个用C实现遗传算法的程序 an example of a very simple genetic algorithm in C and a GA implementation using binary and real coded variables

/* ga.c is an example of a very simple genetic algorithm in C */ & /***************************...
  • amu0421
  • amu0421
  • 2011年05月26日 18:23
  • 788
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Example Of has_many In Rails's ActiveRecord
举报原因:
原因补充:

(最多只允许输入30个字)