在activeRecord中手动管理连接表关联

时间:2017-10-02 13:45:51

标签: ruby postgresql activerecord ruby-on-rails-5

我有三个表,服务器,域名,网站,我正在努力使它如此工作:

您创建一个站点,然后创建主域和别名,并将主域分配给站点(通过域页中的下拉框选择一个站点)。

然后在服务器中,您选择一个站点,它会自动将该站点的所有域(包括别名)分配给该服务器。

Domains表也用作站点和服务器表的直通表

但是目前,当您选择一个网站然后保存它时,它会自动生成关系,而不是使用已存在的关系。

同样由于某些原因,无论我如何尝试,activeRecord都不允许我将域分配给服务器:

public class QuestionConfiguration : EntityTypeConfiguration<Question>
{
    public QuestionConfiguration()
    {
        Property(p => p.Statement).HasColumnName("STATEMENT");

        // Configures the TPH
        Map<VisualQuestion>(p => p.Requires("TYPE").HasValue("Visual").HasMaxLength(10));
        Map<ObjectiveQuestion>(p => p.Requires("TYPE").HasValue("Objective").HasMaxLength(10));
        Map<DiscursiveQuestion>(p => p.Requires("TYPE").HasValue("Discursive").HasMaxLength(10));

        ToTable("TB_QUESTION");
    }
}

public class DiscursiveQuestionConfiguration : Configuration<DiscursiveQuestion>
{
    public DiscursiveQuestionConfiguration()
    {
    }
}

public class VisualQuestionConfiguration : Configuration<VisualQuestion>
{
    public VisualQuestionConfiguration()
    {
    }
}

public class ObjectiveQuestionConfiguration : Configuration<ObjectiveQuestion>
{
    public ObjectiveQuestionConfiguration()
    {
    }
}

public class ObjectiveQuestionOptionConfiguration : Configuration<ObjectiveQuestionOption>
{
    public ObjectiveQuestionOptionConfiguration()
    {
        HasRequired(p => p.Question).WithMany(p => p.Options).HasForeignKey(p => p.ObjectiveQuestionId);

        Property(p => p.ObjectiveQuestionId).HasColumnName("ID_OBJECTIVE_QUESTION");
        Property(p => p.Statement).HasColumnName("STATEMENT"); // <--- This doesnt get mapped! :(

        ToTable("TB_OBJECTIVE_QUESTION_OPTION");
    }
}
尽管没有错误,服务器上的

protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Approach 1: This works //modelBuilder.Entity<Question>().Map<DiscursiveQuestion>(p => p.Requires("TP_QUESTION").HasValue("D")).ToTable("TB_QUESTION"); //modelBuilder.Entity<Question>().Map<ObjectiveQuestion>(p => p.Requires("TP_QUESTION").HasValue("O")).ToTable("TB_QUESTION"); //modelBuilder.Entity<Question>().Map<VisualQuestion>(p => p.Requires("TP_QUESTION").HasValue("V")).ToTable("TB_QUESTION"); // Approach 2: This does work too, however ObjectiveQuestionOption* does not inherit the statement column modelBuilder.Configurations.Add(new QuestionConfiguration()); modelBuilder.Configurations.Add(new QuestionOptionConfiguration()); } 仍为零(成功保存,只有server.domains = domains server.domains_id = domains.map(:&id) server[:domains_id] = domains.map(:&id) server.assign_attribute(:domains, domains) server.assign_attribute(:domains_id, domains.map(:&id) 为零)

最终,我想知道的是如何手动管理连接表以及为什么不让rails为我赋予属性值?

server.rb:

domains_id

domain.rb:

domains_id

site.rb:

class Server < ApplicationRecord

  before_save :set_domains#, if: :sites_id_changed?
  has_many :domains, dependent: :nullify
  has_many :sites, through: :domains
  has_many :projects, through: :sites
  has_many :clients, through: :projects


  def set_domains
    domains = get_domains Site.where(id: self.site_ids).all
    domains += get_domains domains,:domains
    domains.each do |domain|
      domain.server = self
    end
    self.domains = domains
  end


  private
    def get_domains(objects,meth=:domain)
      objects.first.blank? ? [] : objects.map(&meth).flatten
    end
end

0 个答案:

没有答案