在ActiveRecord模型复杂的关联 [英] Complex associations in ActiveRecord models

查看:161
本文介绍了在ActiveRecord模型复杂的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何与各协会,比简单的更复杂的ActiveRecord便宜的价格的has_many belongs_to的,等。

I'm trying to understand how ActiveRecord deals with associations that are more complex than simple has_many, belongs_to, and so on.

作为一个例子,考虑用于记录音乐演出的应用程序。每个千兆有一个乐队,其中有一个流派。每个千兆也有一个场地,其中有一个区域。

As an example, consider an application for recording music gigs. Each Gig has a Band, which has a Genre. Each Gig also has a Venue, which has a Region.

在MS的访问(这我突然开始觉得挺怀念)粗糙的符号,这些关系将psented这样的$ P $

In the rough notation of MS Access (which I'm suddenly beginning to feel quite nostalgic for) these relationships would be presented like this

      1  ∞      1  ∞     ∞  1       ∞  1
Genre ---- Band ---- Gig ---- Venue ---- Region

我希望能够找出来,例如,所有谁一直在一个地区发挥承载一定的风格的乐队,或者所有的场馆。

I would like to be able to find out, for example, all the bands who've played in a region, or all the venues that host a certain genre.

在理想情况下,我的模型将包含此code

Ideally, my models would contain this code

class Genre
  has_many :bands
  has_many :gigs, :through => bands
  has_many :venues, :through => :gigs, :uniq => true
  has_many :regions, :through => :venues, :uniq => true
end

class Band
  belongs_to :genre
  has_many :gigs
  has_many :venues, :through => :gigs, :uniq => true
  has_many :regions, :through => :venues, :uniq => true
end

class Gig
  belongs_to :genre, :through => :band
  belongs_to :band
  belongs_to :venue
  belongs_to :region, :through => :venue
end

等了地点区域

不过,看来我不得不产生这样的事情,而不是

However, it seems I have to produce something like this instead

class Genre
  has_many :bands
  has_many :gigs, :through => bands
  has_many :venues, :finder_sql => "SELECT DISTINCT venues.* FROM venues " +
    "INNER JOIN gigs ON venue.id = gig.venue_id " +
    "INNER JOIN bands ON band.id = gig.band_id " +
    "WHERE band.genre_id = #{id}"
  # something even yuckier for regions
end

class Band
  belongs_to :genre
  has_many :gigs
  has_many :venues, :through => :gigs, :uniq => true
  # some more sql for regions
end

class Gig
  delegate :genre, :to => :band
  belongs_to :band
  belongs_to :venue
  delegate :region, :to => :venue
end

我有两个问题 - 一个总与一个特定

I have two questions - one general and one particular.

一般的:

我本来以为我要怎样做会拿出相当频繁。是我真正做到这一点的最好办法,或者是有什么更简单的说,我可以俯瞰?

I would have thought that what I was trying to do would come up fairly often. Is what I have really the best way to do it, or is there something much simpler that I'm overlooking?

具体:

上面我所不其实挺正常工作!该#{ID} 在第二个流派模式实际上返回类的ID。 (我觉得)。然而,这似乎工作这里和<一href="http://stackoverflow.com/questions/198831/activerecord-association-question-getting-hasmany-through-to-work/198971#198971">here

What I have above doesn't actually quite work! The #{id} in the second genre model actually to return the id of the class. (I think). However, this seems to work here and here

我意识到这是一个相当史诗的问题,所以谢谢你,如果你已经不远了这一点。任何帮助将大大AP preciated!

I realise this is a rather epic question, so thank you if you've got this far. Any help would be greatly appreciated!

推荐答案

协会被设计成可读的和可写的。他们的价值有很大一部分是你可以做这样的事情:

Associations are designed to be readable and writable. A large part of their value is that you can do something like this:

@band.gigs << Gig.new(:venue => @venue)

这听起来,不过,像你想的是只读的东西。换句话说,要场地和流派联系在一起,但你永远不会做的:

It sounds, though, like you want something that's read-only. In other words, you want to associate Venues and Genres, but you'd never do:

@venue.genres << Genre.new("post-punk")

,因为这没有任何意义。 A点只有一个类型,如果一个频段,具有特定的类型有千兆那里。

because it wouldn't make sense. A Venue only has a Genre if a Band with that particular Genre has a Gig there.

协会不为工作,因为他们必须是可写的。下面是我会怎么做只读协会:

Associations don't work for that because they have to be writable. Here's how I'd do readonly associations:

class Genre
  has_many :bands 

  def gigs
    Gig.find(:all, :include => 'bands', 
             :conditions => ["band.genre_id = ?", self.id])
  end

  def venues 
    Venue.find(:all, :include => {:gigs => :band}, 
      :conditions => ["band.genre_id = ?", self.id])
  end
end

这篇关于在ActiveRecord模型复杂的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆