• Rails 实现多对多自连接


    1. 先生成关联代码

    class WorkConnect < ActiveRecord::Base                          
      belongs_to :working, class_name: "Company"             
      belongs_to :worked, class_name: "Company"                                            
    end                         
    
    class Company < ActiveRecord::Base
      #working表示我合作的公司,worked表示跟我合作的公司
      has_many :working_c, class_name: "WorkConnect", foreign_key: :worked_id
      has_many :worked_c, class_name: "WorkConnect", foreign_key: :working_id
      has_many :working, class_name: "Company", through: :working_c
      has_many :worked, class_name: "Company", through: :worked_c
    end     

    2. 创建WorkConnect中两个字段

    class CreateWorkConnect < ActiveRecord::Migration
      def change
        create_table :work_connects do |t|
          t.integer :worked_id
          t.integer :working_id
        end
      end
    end

    3. 然后进入console,调试,可以创建成功。

    2.3.0 :003 > c1 = Company.find(2)
      Company Load (0.2ms)  SELECT  `companies`.* FROM `companies` WHERE `companies`.`id` = 2 LIMIT 1
     => #<Company id: 2, name: "Eastcompeace", created_at: "2016-03-21 04:50:28", updated_at: "2016-03-21 04:50:28", work_id: nil>
    2.3.0 :004 > c2 = Company.find(5)
      Company Load (0.2ms)  SELECT  `companies`.* FROM `companies` WHERE `companies`.`id` = 5 LIMIT 1
     => #<Company id: 5, name: "360", created_at: "2016-03-21 06:53:37", updated_at: "2016-03-21 06:53:37", work_id: nil>
    2.3.0 :005 > c1.working
      Company Load (0.4ms)  SELECT `companies`.* FROM `companies` INNER JOIN `works` ON `companies`.`id` = `works`.`working_id` WHERE `works`.`worked_id` = 2
     => #<ActiveRecord::Associations::CollectionProxy []>
    2.3.0 :006 > c1.working << c2
       (0.2ms)  BEGIN
      SQL (0.3ms)  INSERT INTO `works` (`worked_id`, `working_id`) VALUES (2, 5)
       (67.9ms)  COMMIT
     => #<ActiveRecord::Associations::CollectionProxy [#<Company id: 5, name: "360", created_at: "2016-03-21 06:53:37", updated_at: "2016-03-21 06:53:37", work_id: nil>]>
    2.3.0 :007 > c1.worked
      Company Load (0.5ms)  SELECT `companies`.* FROM `companies` INNER JOIN `works` ON `companies`.`id` = `works`.`worked_id` WHERE `works`.`working_id` = 2
     => #<ActiveRecord::Associations::CollectionProxy []>
    2.3.0 :008 > c1.working
     => #<ActiveRecord::Associations::CollectionProxy [#<Company id: 5, name: "360", created_at: "2016-03-21 06:53:37", updated_at: "2016-03-21 06:53:37", work_id: nil>]>
    2.3.0 :009 > c1.save
       (0.2ms)  BEGIN
       (0.1ms)  COMMIT
     => true
    2.3.0 :010 > c2.worke
    NoMethodError: undefined method `worke' for #<Company:0x00000003c33e00>
    2.3.0 :011 > c2.worked Company Load (0.5ms) SELECT `companies`.* FROM `companies` INNER JOIN `works` ON `companies`.`id` = `works`.`worked_id` WHERE `works`.`working_id` = 5 => #<ActiveRecord::Associations::CollectionProxy [#<Company id: 3, name: "tengxun", created_at: "2016-03-21 05:56:22", updated_at: "2016-03-21 05:56:22", work_id: nil>, #<Company id: 2, name: "Eastcompeace", created_at: "2016-03-21 04:50:28", updated_at: "2016-03-21 04:50:28", work_id: nil>]> 2.3.0 :012 > c2.working Company Load (0.4ms) SELECT `companies`.* FROM `companies` INNER JOIN `works` ON `companies`.`id` = `works`.`working_id` WHERE `works`.`worked_id` = 5 => #<ActiveRecord::Associations::CollectionProxy []>

    该例子纯属练习理解Model的关联,不考虑业务需求,欢迎指正。

  • 相关阅读:
    设计模式:访问者模式
    设计模式:模板模式
    三分法——求解凸性函数的极值问题——czyuan原创
    素数&&Miller_Rabbin
    【算法入门】深度优先搜索(DFS)
    快速幂取模
    hrbeu1280Turn the corner
    hdoj_3400Line belt
    【专题】三分法和牛顿迭代法总结
    zoj_3203Light Bulb
  • 原文地址:https://www.cnblogs.com/zhangyanpei/p/5302119.html
Copyright © 2020-2023  润新知