Область с двумя внешними ключами - программирование

Область с двумя внешними ключами

У меня есть следующая схема: enter image description here

Я хочу иметь возможность вызывать proposals для foreign_keys (author_id и editor_id) также для отдельных (например, author_proposals и editor_proposals), и мне нужно иметь возможность ленивы или нетерпеливо загружают их (например, User.includes(:proposals) или без него с joins).

Обновление:

#I have the scopes which is like this:
class User < ActiveRecord::Base
  has_many :author_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editor_proposals, class_name: 'Proposal', foreign_key: :editor_id
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id
end

Но мне нужен универсальный, который даст мне все предложения (как author_proposals, так и editor_proposals), которые он также будет загружать. Должен ли я использовать условия на has_many?

4b9b3361

Ответ 1

Вы можете сделать что-то вроде:

class User < ActiveRecord::Base
  has_many :authored_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editored_proposals, class_name: 'Proposal', foreign_key: :editor_id

  def proposals
    authored_proposals | editored_proposals
  end
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id

  def users
    author | editor
  end
end

Вы можете запросить загрузку proposals, выполнив: User.includes(:authored_proposals, :editored_proposals). Это не чисто путь рельсов, но кажется мне более чистым.



Вы также можете сделать:

class User < ActiveRecord::Base
  has_many :authored_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editored_proposals, class_name: 'Proposal', foreign_key: :editor_id

  has_many : proposals, finder_sql: proc { "SELECT * FROM proposals WHERE (proposals.author_id = #{id} or proposals. editor_id = #{id})" }
end

Ответ 2

Я бы сделал что-то вроде этого:

class User < ActiveRecord::Base
  has_many :authored_proposals, class_name: 'Proposal', foreign_key: :author_id
  has_many :editored_proposals, class_name: 'Proposal', foreign_key: :editor_id

  def proposals
    Proposal.where('author_id = :id OR editor_id = :id', { id: id }).distinct
  end
end

class Proposal < ActiveRecord::Base
  belongs_to :author, class_name: 'User', foreign_key: :author_id
  belongs_to :editor, class_name: 'User', foreign_key: :editor_id

  def users
    User.where(id: [author_id, editor_id].uniq)
  end
end

Ответ 3

Задайте свои ассоциации следующим образом:

class User < ActiveRecord::Base
  has_many :author_proposals, :class_name => "Proposal", :foreign_key => "author_id"
  has_many :editor_proposals, :class_name => "Proposal", :foreign_key => "editor_id"
end

class Proposal < ActiveRecord::Base
  belongs_to :author, :class_name => 'User', :foreign_key => "author_id"
  belongs_to :editor, :class_name => 'User', :foreign_key => "editor_id"
end