【问题标题】:Rails 4: how to implement routes for a many-to-many relationship?Rails 4:如何实现多对多关系的路由?
【发布时间】:2015-09-24 08:00:10
【问题描述】:

在我们的 Rails 4 应用程序中,有四种模型:

class User < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :calendars, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

class Post < ActiveRecord::Base
    belongs_to :calendar
end

以下是相应的迁移:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
      t.integer :total_calendar_count
      t.integer :owned_calendar_count

      t.timestamps null: false
    end
  end
end

class CreateAdministrations < ActiveRecord::Migration
  def change
    create_table :administrations do |t|
      t.references :user, index: true, foreign_key: true
      t.references :calendar, index: true, foreign_key: true
      t.string :role

      t.timestamps null: false
    end
  end
end

class CreateCalendars < ActiveRecord::Migration
  def change
    create_table :calendars do |t|
      t.string :name

      t.timestamps null: false
    end
  end
end

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
        t.references :calendar, index: true, foreign_key: true
        t.date :date
        t.time :time
        t.string :focus
        t.string :format
        t.string :blog_title
        t.text :long_copy
        t.text :short_copy
        t.string :link
        t.string :hashtag
        t.string :media
        t.float :promotion
        t.string :target
        t.integer :approval
        t.text :comment

      t.timestamps null: false
    end
  end
end

这里的技巧是我们有一个多对多的关系,用户有很多日历,日历有很多用户,两者都通过管理连接表。

我们需要同时显示:

  • 给定用户的日历列表
  • AND 给定日历的用户列表(来自给定用户的会话,假设用户只能看到属于他的日历)

首先,我们考虑将resources 嵌套在routes 文件中,例如这样:

resources :users do
  resources :administrations
  resources :calendars
end

但后来我们想知道我们是否也可以反过来嵌套它们,就像这样:

resources :calendars do
      resources :administrations
      resources :users
    end

这会让我们实现我们所需要的吗?这是一个好的做法(甚至可能)吗?

如果不是,我们应该如何构建我们的路线?

更新:下面的路由结构呢:

resources :users do
  resources :administrations
end

resources :calendars do
  resources :administrations
end

我们可以将一个资源嵌套到两个不同的其他资源中吗?

【问题讨论】:

  • 这是一个包含很多问题的问题...也许您可以将其分解为离散的较小问题,以便更容易理解您要完成的任务。
  • 你完全正确,对此感到抱歉。我刚刚更新了问题。

标签: ruby-on-rails ruby-on-rails-4 routing routes rails-routing


【解决方案1】:

您可以使用Routing Concerns 将一个资源嵌套到两个或多个其他资源中。例如:

concern :administratable do
  resources :administrations
end

resources :users, concerns: :administratable
resources :calendars, concerns: :administratable

【讨论】:

  • 谢谢。如果我理解正确,这会让我们设置诸如users/:user_id/administrations/:administration_idcalendars/:calendar_id/administrations/:administration_id 之类的路由,对吧?
  • 另外,使用这段代码,我们是否应该从routes 文件中删除resources :usersresources :calendars?换句话说,您推荐的代码是替换routes 文件中资源的初始声明还是对其进行补充?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多