2011-03-18 84 views
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 

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

回答

0

好的2个问题。

1 - 保存是否返回真或假?

2 - 如果为false src.errors.full_messages返回什么?

+0

它在srcpkg和binpkg中返回false,架构已创建,并且srcpkg.errors.full_messages ='架构空白' – P0w3r3d 2011-03-19 04:17:00

+0

嗯让我。我的建议是使用rails控制台,并尝试填充方法中的每一行,以准确跟踪进入的内容和失败的位置。来自您在validate_presence验证之一中失败的消息。 – huntsfromshadow 2011-03-19 15:22:52

+0

是的,但我不知道为什么'建筑空白'的信息...我通过arch.id(其中有数字),并且出现该消息:D – P0w3r3d 2011-03-19 21:57:59

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 

I'm!验证消息,但..此代码保存失败!返回“验证失败:架构不能为空”,但logger.debug返回一个有效的数字..它怎么会?