Ruby on rails开发从头来(五十七)- ActiveRecord基础(多对多关联关系)

61 篇文章 0 订阅
61 篇文章 0 订阅
在Rails中多对多关联通过在关联表对应的类中声明has_and_belongs_to_many来实现。

在数据库中,多对多关联使用中间表来实现,表中包括关联表的主键,Active Record假定这个中间表的名字是由关联表的名字根据字母的顺序串联起来得到的。例如,关联表为categories和products,中间表的名字就是categories_products。


    注意我们的关联表没有id列,有两个原因,首先,不需要一个唯一的标识来识别两个外键之间的连接,我们定义表的语句像下面这样:

create table categories_products (

category_id int not null,

product_id int not null,

constraint fk_cp_category foreign key (category_id) references categories(id),

constraint fk_cp_product foreign key (product_id) references products(id),

primary key (category_id, product_id)

);

第二个原因在中间表中不包括一个id列,Active Record在访问某个行时会自动包含所有的列。如果包含了一个id列,那么这个id列就会复写掉在关联表中的id列。

 

The has_and_belongs_to_many() 声明

has_and_belongs_to_many在很多方面很像has_many,has_and_belongs_to_many创建了本质上是一个集合的属性,该属性支持和has_many相同的方法。

也许我们使用Rails来写一个社区站点,在这里用户可以阅读文章。这里有很多的用户和文章,而且任何一个用户都可以阅读多个文章,为了跟踪,我们希望知道谁读了哪些文章,每篇文章有谁阅读过,我们也希望知道用户最后一次在什么时间阅读了哪篇文章,我们会这样设计表:

  

我们这样设置两个Model类互相关联:

class Article < ActiveRecord::Base

has_and_belongs_to_many :users

# ...

end

class User < ActiveRecord::Base

has_and_belongs_to_many :articles

# ...

end

这样我们就可以列出所有阅读过文章123的用户和名为pragdave的用户阅读的所有文章:

# Who has read article 123?

article = Article.find(123)

readers = article.users

# What has Dave read?

dave = User.find_by_name("pragdave")

articles_that_dave_read = dave.articles

当我们的程序通知某个人阅读了某篇文章的时候,将user记录和article记录建立关联,我们调用下面的方法:

class User < ActiveRecord::Base

has_and_belongs_to_many :articles

def read_article(article)

articles.push_with_attributes(article, :read_at => Time.now)

end

# ...

end

方法push_with_attributes( )和<<方法的作用一样,都是给两个Model之间设置连接,而且还赋值给中间表记录什么人在什么时间阅读了文章。

注:如果该方法难以理解,可以想象一下C#中使用反射给某个对象的字段赋值,我们需要提供对象,对象的字段名,字段对应的值来进行操作。

作为一种的关联方法,has_and_belongs_to_many支持一系列声明来复写Active Record的默认设置::class_name, :foreign_key和:conditions,和其他的has_方法一样(:foreign_key设置中间表中的外键的名字)。进一步说,has_and_belongs_to_many支持复写中间表的名字,外键列的名字,find,insert,delete中使用的SQL,详细请参考Rdoc。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值