【问题标题】:Many-to_many relationship in Ruby on RailsRuby on Rails 中的多对多关系
【发布时间】:2013-02-04 15:07:08
【问题描述】:

我一直在尝试在 Ruby on Rails 中创建网络应用程序,它会收集不同餐厅的菜单并将其显示在我的应用程序中。我想创建多对多关联,但我在那里有点困惑。我搜索了多对多关系并阅读了有关 HABTM 或 has_many/belongs_to 的信息,仍然有点困惑。 我的课:

餐厅 = {名称}

菜单 = {名称,价格}

并加入表:

menu_restaurant = {日期}

我在这里很困惑。我读过我不能在连接表中使用带有额外值的 HABTM。而且我不应该有使用 HABTM 连接表的模型。

我目前的型号: app/models/menu.rb

class Menu < ActiveRecord::Base
  attr_accessible :cenaStudent, :name
  has_many :restaurants, dependent: :destroy
  validates :priceStudent, presence: true
  validates :name, presence:true
end

app/models/restaurant.rb

class Restaurant < ActiveRecord::Base
  attr_accessible :name
  has_many :menus, dependent: :destroy

  validates :name, presence: true 
end

app/models/menu_restaurant.rb

class MenuRestaurant < ActiveRecord::Base
  attr_accessible :date
end

这些是我的数据库表 db/migrate/{timestamp}_create_restaurants.rb 和类似的 {...}menu.rb

class CreateRestaurants < ActiveRecord::Migration
  def self.up
    create_table :restaurants do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :restaurants
  end
end

db/migrate/{timestamp}_create_menus_restaurants.rb

class CreateMenusRestaurants < ActiveRecord::Migration
  def up
    #create the association table
    create_table :menus_restaurants, :id => false do |t|
      t.integer :menu_id, :null => false
      t.integer :restaurant_id, :null => false
      t.date :date
    end

    #add index
    add_index :menus_restaurants, [ :menu_id, :restaurant_id]

  end

  def down
    remove_index :menus_restaurants, :column => [ :menu_id, :restaurant_id]
    drop_table :menus_restaurants
  end
end

我应该使用 ruby​​ on rails 中的什么关系?我需要什么型号?

我还有一个与 RoR MVC 架构相关的问题。因为有关系,所以就写在这里。

我在 ruby​​ 中创建了一个函数,用于下载包含菜单列表的 pdf 文件。解析菜单名称和价格。餐厅名称与 pdf 文件匹配。所以我有数据需要将它们插入到数据库中。我该怎么做?在哪里做?我猜有些部分应该在控制器中,函数应该在 lib 目录中?这是我第一个使用任何架构的应用程序。 这也是我的第一个 stackoverflow 问题,希望我没有忘记任何事情。

【问题讨论】:

  • 感谢您的回答!有没有机会你也可以帮我解决第二个问题?

标签: ruby-on-rails ruby many-to-many


【解决方案1】:

如果要向关联表添加属性,应使用has_many through 关联。

class Menu < ActiveRecord::Base
 has_many :menu_restaurants
 has_many :restaurant, :through => :menu_restaurants
 attr_accessible :cenaStudent, :name
 validates :priceStudent, presence: true
 validates :name, presence:true
 ...
end

class MenuRestaurant < ActiveRecord::Base
 belongs_to :menu
 belongs_to :patient
 attr_accessible :date
 ...
end

class Restaurant < ActiveRecord::Base
  has_many :menu_restaurants
  has_many :menus, :through => :menu_restaurants
  validates :name, presence: true 
end

【讨论】:

    【解决方案2】:

    我想你差不多了,你的迁移和模型似乎没问题,除了处理连接表 :menu_restaurants。

    我已经修改了你的模型,以便连接表可以连接所有内容

    class Menu < ActiveRecord::Base
      attr_accessible :cenaStudent, :name
      has_many :menu_restaurants
      has_many :restaurants, through: :menu_restaurants , dependent: :destroy
      validates :priceStudent, presence: true
      validates :name, presence:true
    end
    
    class Restaurant < ActiveRecord::Base
      attr_accessible :name
      has_many :menu_restaurants
      has_many :menus, through: :menu_restaurants , dependent: :destroy
      validates :name, presence: true 
    end
    
    class MenuRestaurant < ActiveRecord::Base
      attr_accessible :date
      belongs_to :menu
      belongs_to :restaurant
    end
    

    另外我认为你的连接表应该被称为 menu_restaurants (单数菜单)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多