多模型无法工作的 Rails 问题

发布于 2024-10-23 19:48:17 字数 3027 浏览 1 评论 0原文

我正在尝试处理 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

我现在搜索了一个多星期并尝试了其他方法..但没有人工作..所以,有什么想法吗? 多谢

I'm trying to process the dsc files from an ubuntu's sourcecode repository to populate a rails application, for this I used 3 models:

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

and using a controller admin with:

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

this doesn't create neither the srcpkg object, nor the binspkgs objects
before this, I also tried this:

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])

even with:

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

I searched for than a week now and tried other methods.. but no one worked.. so, any idea?
thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2024-10-30 19:48:17

我更改了代码..现在,如果没有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 返回一个有效的数字..怎么可能?

I changed the code.. now, without update_attributes it creates some binpkgs but no srcpkgs or other binpkgs objects like this where I set the 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

I´m using the save! to validate the messages, but.. this code failed in save! returning "Validation failed: Architecture can't be blank" , but the logger.debug returns a valid number .. how can it be??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文