2011-03-18 121 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返回一個有效的數字..它怎麼會?