rails problem with multi model not working
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
log开发者_C百科ger.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
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??
精彩评论