Я пытаюсь обработать файлы dsc из репозитория исходного кода Ubuntu для заполнения приложения 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
Я искал больше недели и пробовал другие методы ... но никто не работал .. так, есть идеи?
большое спасибо