多模型无法工作的 Rails 问题
我正在尝试处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更改了代码..现在,如果没有update_attributes,它会创建一些binpkgs,但不会创建srcpkgs或其他binpkgs对象,就像我设置architecture_id的地方:
我正在使用保存!验证消息,但是..此代码保存失败!返回“验证失败:架构不能为空”,但是 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:
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??