导轨问题多模型无法正常工作

时间:2011-03-18 17:47:31

标签: ruby-on-rails ruby-on-rails-3 nested-forms

我正在尝试从ubuntu的源代码存储库处理dsc文件以填充rails应用程序,为此我使用了3个模型:

class Architecture < ActiveRecord::Base
  has_many :srcpkgs, :dependent => :destroy
  has_many :binpkgs, :through => :srcpkgs, :dependent => :destroy

  accepts_nested_attributes_for :srcpkgs, :allow_destroy => true
  accepts_nested_attributes_for :binpkgs, :allow_destroy => true
  validates_presence_of :name
  validates_uniqueness_of :name
end

class Srcpkg < ActiveRecord::Base
  has_many :binpkgs, :dependent => :delete_all
  belongs_to :architecture

  accepts_nested_attributes_for :binpkgs
  attr_accessor :architecture_id, :bdeps
  validates_presence_of :architecture_id
end

class Binpkg < ActiveRecord::Base
  belongs_to :srcpkg, :touch => true
  belongs_to :architecture

  accepts_nested_attributes_for :srcpkg
  accepts_nested_attributes_for :architecture
  attr_accessor :architecture_id, :srcpkg_id
  validates :architecture_id, :presence => true
end

并使用控制器管理员:

def populate
  notice = nil
  pname = '/tmp/dpkg_1.15.5.6ubuntu4.5.dsc'
  p = Pkg.new pname
  binpkg = []
  bdep = []
  if not Srcpkg.find_by_name(p.source.to_s)
    arch = Architecture.find_or_create_by_name(p.architecture.to_s)
    arch.save
    srcpkg = Srcpkg.find_or_initialize_by_name(p.source.to_s)
    p.bdepends.each do |b|
      b1 = Binpkg.find_or_initialize_by_name(b)
      b1.save
      bdep.push(b1.id)
    end
    srcpkg.update_attributes({:name => p.source.to_s,
                           :version => p.version.to_s,
                           :stdversion => p.stdversion.to_s,
                           :bdeps => bdep,
                           :arquitecture_id => arch.id})
    srcpkg.save
    p.binary.each do |b|
      b1 = Binpkg.create
      b1.name = b
      b1.srcpkg_id = srcpkg.id
      b1.arquitecture_id = arch.id
      b1.save
    end
    notice = "Package successfully processed"
    logger.debug " ---- here #2 ---- "
  else
    logger.debug " ---- here #3 ---- "
    notice = "Package not processed, it was already added"
  end
  flash[:notice] = notice
  redirect_to "/architectures/#{arch.id}"
end

这既不会创建srcpkg对象,也不会创建binspkgs对象 在此之前,我也试过这个:

p = Pkg.new pname
params = { :architecture => {
    :name => p.architecture.to_s,
    :srcpkgs_attributes => [{
        :name => p.source.to_s,
        :version => p.version.to_s,
        :stdversion => p.stdversion.to_s,
        :bdeps => [],
        :binpkgs_attributes => []
    }]
  }
}
p.binary.each do |b|
  params[:architecture][:srcpkgs_attributes][0][:binpkgs_attributes] << {:name => b.to_s}
end
if not Srcpkg.find_by_name(p.source.to_s)
  arch = Architecture.find_or_create_by_name(p.architecture.to_s)
  arch.update_attributes(params[:architecture])

即使:

src = Srcpkg.new(params[:architecture][:srcpkgs_attributes][0])
src.save

我现在搜索了一个星期并尝试了其他方法..但没有人工作..所以,任何想法? 非常感谢

1 个答案:

答案 0 :(得分:0)

我更改了代码..现在,没有update_attributes它会创建一些binpkgs但没有srcpkgs或其他binpkgs对象,我设置了architecture_id:

p.binary.each do |b|
  if not Srcpkg.find_by_name(b)
    b1 = Binpkg.new
    b1.name = b
    #b1.srcpkg_id = srcpkg.id
    b1.arquitecture_id = arch.id
    logger.debug "b1.arquitecture_id = '#{b1.arquitecture_id}'"
    b1.save!
    logger.debug b1.id
  else
    if Srcpkg.find_by_name(b).srcpkg_id.empty?
      b1 = Srcpkg.find_by_name(b)
      b1.srcpkg_id = srcpkg.id
      b1.save!
    end
  end
end
我正在使用保存!验证消息,但..此代码保存失败!返回“验证失败:架构不能为空”,但logger.debug返回一个有效的数字..它怎么可能?