【问题标题】:has_many through - Notes sender and receiverhas_many through - 备注发送者和接收者
【发布时间】:2018-07-20 11:45:40
【问题描述】:

我正在开发一个具有三个模型的 Rails 应用程序。

class User < ApplicationRecord; end
class Share < ApplicationRecord; end
class Note < ApplicationRecord; end

create_table :users do |t|
  t.timestamps
end

create_table :notes do |t|
      t.integer 'user_id'
      t.text 'title'
      t.text 'short_description'
      t.string 'name'
      t.timestamps
end

create_table :shares do |t|
  t.integer 'user_id'
  t.integer 'receiver_id'
  t.integer 'note_id'
  t.timestamps
end

我怎样才能在它们之间建立关联,这样我才能得到

  • 用户 A 共享的笔记。
  • 用户 A 收到的备注。
  • 用户 A 创建的笔记。

【问题讨论】:

    标签: ruby-on-rails activerecord associations


    【解决方案1】:

    @Mehmet Adil İstikbal 给出了部分答案,所以我会尝试完成它。

    这是另一种仅使用关联的方法:

    class User < ApplicationRecord
      has_many :created_notes, class_name: 'Note', foreign_key: :user_id
    
      has_many :received_shares, foreign_key: :receiver_id, class_name: 'Share'
      has_many :received_notes, through: :received_shares, source: :note
    
      has_many :shares
      has_many :shared_notes, through: :shares, source: :note
    end
    
    class Share < ApplicationRecord
      # Optional
      belongs_to :creator, class_name: 'User', foreign_key: :user_id
      belongs_to :receiver, class_name: 'User', foreign_key: :receiver_id
      # Mandatory
      belongs_to :note
    end
    
    class Note < ApplicationRecord ; end
    
    user_a = User.first
    user_a.shared_notes
    user_a.received_notes
    user_a.created_notes
    

    如果您选择@Mehmet Adil İstikbal 答案,请确保转换 user.shares.each {|share| share.note}user.shares.map(&amp;:note)(使用地图而不是每个地图)

    我的答案使用 has_many through 关联,它允许您“通过”连接表。

    【讨论】:

    • user_a.received_notes 抛出错误NameError (uninitialized constant User::ReceivedShare)
    • 抱歉,我忘记了这些行中的 class_name
    【解决方案2】:

    在用户模型中你可以这样做:

    has_many :shares, foreign_key: 'user_id', class_name: 'Share', dependent: :destroy has_many :receives, foreign_key: 'receiver_id', class_name: 'Share', dependent: :destroy

    你可以这样调用:

    User.first.shares.each {|share| share.note}

    这将获得与第一个用户 ID 及其所有笔记的所有共享。 对于接收者:

    User.first.receives.each {|share| share.note}

    在您的共享模型中,您还可以像这样指定相反的连接:

    belongs_to :sender, foreign_key: 'user_id', class_name: 'User'

    belongs_to :receiver, foreign_key: 'receiver_id', class_name: 'User'

    有了这个你可以调用:

    Share.first.receiver 这会将您带到收到此帖子的用户

    对于用户创建的笔记,您可以调用:

    User.first.notes

    您可能希望删除这些依赖项以便您的项目。 希望对你有帮助

    【讨论】:

    • 感谢您的回答,让我试试,会告诉你的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2023-03-03
    • 2015-07-28
    • 1970-01-01
    • 2016-09-04
    相关资源
    最近更新 更多