宝典:第八式 表间关联NORMAL

首先我们继续在easy里面提到的一对多的 Questions-Answers
这里已经有了题目了,有题目了接下来要干什么咧,  做题目
这里我们就加入Users 与 notes
notes记录谁做过那个题,选择的答案
关系就这样理解
[size=large][color=red]这个图片怎么传上去啊???[/color][/size]
[img]C:\Documents and Settings\Administrator\桌面\xialas.bmp[/img]

然后是加入关联代码

几个模型中分别为

class Answer < ActiveRecord::Base
belongs_to :question
has_many :users , :through => :notes
has_many :notes , :dependent => :destroy
end

class Question < ActiveRecord::Base
has_many :answers ,:dependent => :destroy
has_many :notes
has_many :users , :through => :notes
end

class User < ActiveRecord::Base
has_many :notes
has_many :questions , :through => :notes
has_many :answers , :through => :notes
end

class Note< ActiveRecord::Base
belongs_to :question
belongs_to :answer
belongs_to :user
end



上面完成的都是等同与配制比较枯燥,下面的将是神奇的了,用"敏捷之道"的话说,就是魔法即将来临.

##只需要取出相应的字段后下面即可实现
@user.questions ##这个人做过的所有题目(纪录集)
@user.answers      ##这个用户选择过的所有答案(纪录集)
@user.notes       ##这个用户的所有做题纪录(纪录集)
@question.users     ##做过这个题目的所有人(纪录集)
@answer.users      ##选择过这个答案的所有人(纪录集)


不知道表达得怎么样,有什么不懂欢迎留言提问,或者去[url]http://sayid2008.iteye.com/blog/157405[/url]看看
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值