如何创建“双面” Rails 中的多对多关系?

发布于 2024-08-21 11:11:08 字数 1146 浏览 5 评论 0原文

假设我们有一个摄影网站。任何作者都可以订阅以接收来自任何其他作者的更新。显然,如果作者 A 订阅了作者 B,并不意味着 B 订阅了 A。所以我们构建模型

class Author < ActiveRecord::Base
  has_many :subscriptions
  has_many :subscribed_by_author, :through => :subscriptions, :source => :subscribed_to
end

class Subscription < ActiveRecord::Base
  belongs_to :author
  belongs_to :subscribed_to, :class_name => "Author", :foreign_key => "subscribed_to"
end

这样我们就可以使用

  1. some_author.subscribed_by_author —— some_author 订阅的作者列表。
  2. 对于任何订阅,我们都可以知道两端(谁订阅了谁),

但问题是如何仅使用rails(不使用普通 SQL)获取订阅某个作者的人员列表,即得到以下问题的答案:“谁订阅了某个作者?”

问题:Rails 是否有能力让双方的关系发挥作用,即不仅编写 some_author.subscribed_BY_author,而且还拥有 some_author_subscribed_TO_author?如果有的话,那是什么?

PS 明显的解决方案是

  1. 更改数据库设计,添加名为“direction”的列
  2. 每次创建订阅时创建 2 条记录
  3. 添加到作者模型

    has_many :subscribed_BY_author, :through =>; :订阅,:源=> :subscribed_to, :条件 => “方向=‘按’”

    has_many :subscribed_TO_author, :through =>; :订阅,:源=> :subscribed_to, :条件 => "direction = 'to'"

但我想知道是否有一个不改变数据库设计的解决方案。

Suppose we have a photography site. Any author can subscribe to receive updates from any other author. Obviously if author A is subscribed to author B that doesn't mean that B is subscribed to A. So we build models

class Author < ActiveRecord::Base
  has_many :subscriptions
  has_many :subscribed_by_author, :through => :subscriptions, :source => :subscribed_to
end

class Subscription < ActiveRecord::Base
  belongs_to :author
  belongs_to :subscribed_to, :class_name => "Author", :foreign_key => "subscribed_to"
end

This way we can use

  1. some_author.subscribed_by_author -- the list of the authors to whom some_author is subscribed.
  2. For any subscription we can know both ends (who is subscribed to whom)

But the question is how to get the list of people subscribed to some author using only rails (not using plain SQL) i.e get the answer to :"Who is subscribed to some_author?"

Question: is there any ability in Rails to get the relationship working both sides i.e. not only writing some_author.subscribed_BY_author but having some_author_subscribed_TO_author? If there is one, then what is it?

P.S. Obvious solution is to

  1. Change the database design, adding a column named "direction"
  2. Create 2 records each time a subscription is created
  3. Add to the author model

    has_many :subscribed_BY_author, :through => :subscriptions, :source => :subscribed_to, :conditions => "direction = 'by'"

    has_many :subscribed_TO_author, :through => :subscriptions, :source => :subscribed_to, :conditions => "direction = 'to'"

But i wonder if there is a solution without changing the database design.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-08-28 11:11:08

我会使用普通的 HABTM 来做这样简单的事情,但无论如何你都需要一个连接表。

create_table :subscriptions do |t|
  t.column :author_id, :integer
  t.column :subscriber_id, :integer
end

向作者指出:

class Author < ActiveRecord::Base
  has_and_belongs_to_many :subscribers
    :class_name => "Author",
    :join_table => "subscriptions",
    :association_foreign_key => "subscriber_id"

  def subscriptions # "subscribers" is already included above
    self.subscribers.find(:all, :subscriber_id=>author.id) # hopefully not too 
  end                                                      # much SQL
end

如果您确实致力于方法名称:

  def subscribed_to_author
    subscribers
  end

  def subscribed_by_author(author)
    self.subscribers.find(:all, :subscriber_id=>author.id)
  end

创建一些连接(我会将 SubscriptionsController 设为 RESTy)

SubscriptionsController < ApplicationController
  def create
    @author = Author.find(params[:author_id] # author to be subscribed to
    @user = current_user # user clicking the "subscribe" button

    @author.subscribers << @user # assuming authors should only 
    @author.save                 # be able to subscribe themselves
  end
end

显示名称或其他

@author.subscribers.each do |s|
  s.name
end
# ...or...and...also...
<%= render :partial => @author.subscribers -%>
<%= render :partial => @author.subscriptions -%>

I'd use plain HABTM for something simple like this, but you're going to need a join table no matter what.

create_table :subscriptions do |t|
  t.column :author_id, :integer
  t.column :subscriber_id, :integer
end

Point Author to it:

class Author < ActiveRecord::Base
  has_and_belongs_to_many :subscribers
    :class_name => "Author",
    :join_table => "subscriptions",
    :association_foreign_key => "subscriber_id"

  def subscriptions # "subscribers" is already included above
    self.subscribers.find(:all, :subscriber_id=>author.id) # hopefully not too 
  end                                                      # much SQL
end

If you're really committed to your method names:

  def subscribed_to_author
    subscribers
  end

  def subscribed_by_author(author)
    self.subscribers.find(:all, :subscriber_id=>author.id)
  end

Create some connections (I'd make SubscriptionsController to be RESTy)

SubscriptionsController < ApplicationController
  def create
    @author = Author.find(params[:author_id] # author to be subscribed to
    @user = current_user # user clicking the "subscribe" button

    @author.subscribers << @user # assuming authors should only 
    @author.save                 # be able to subscribe themselves
  end
end

Display names, or whatever

@author.subscribers.each do |s|
  s.name
end
# ...or...and...also...
<%= render :partial => @author.subscribers -%>
<%= render :partial => @author.subscriptions -%>
别想她 2024-08-28 11:11:08
# Author model
has_many :subscriptions_to, :class_name => "Subscription", :foreign_key => "subscribed_to"
has_many :subscribed_to_author, :through => :subscriptions_to, :source => :author

据我所知 - 它有效! :)

# Author model
has_many :subscriptions_to, :class_name => "Subscription", :foreign_key => "subscribed_to"
has_many :subscribed_to_author, :through => :subscriptions_to, :source => :author

As far as I know - it works! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文