Ruby 在 Rails。模范协会
Ruby on Rails. Model Associations
我有一个模型,file.rb
,一个控制器,filecontroller.rb
,以及一组名为 files
的 table 的视图。这样做是为了能够上传 Excel 文件,这样我就可以在索引视图中查看某人上传 Excel 文件的时间、创建时间和个人姓名的列表谁上传了 Excel 文件。
现在,我在名为 bottles
的数据库中有另一个模型 bottle.rb
、一个控制器 bottlecontroller.rb
和一组 table 的视图。数据库中 bottles table 中的列与 Excel 文件相同。
我已经能够在一些验证后上传 excel 文件,以便它们可以进入数据库中的 files
table。我还按照 rails cast 教程从瓶子索引视图中导入 excel 文件并更新我的 bottles
table。
但是,我希望能够上传 Excel 文件并在文件索引视图中导入信息。但是导入还需要更新 bottles 中的信息 table。所以换句话说,从文件索引视图导入的 Excel 需要能够更新 bottle table 数据库。
我知道我需要我的 files.rb
模型和 bottles.rb
模型之间的关联..但是任何帮助都会很棒!
************更新问题********************************* **********
在我的filecontroller.rb
中,我在保存后的创建方法中添加了@file.bottles = Bottle.import(params[:file_url])
。我将 excel 个文件存储在 :file_url
中。我还在 has_many :bottles
的 file.rb
模型中添加了一个关联
def create
.........
..........
if file.save
@file.bottles = Bottle.import(params[:file_url])
end
end
在我的瓶子模型中 bottle.rb
我有
def self.import(file_url)
spreadsheet = Roo::Excel.new(file_url.path, packed:nil,file_warning: :ignore)
.........
bottle.save!
end
但我得到一个未定义的方法 path' for nil:NilClas error in this line
spreadsheet = Roo::Excel.new(file_url.path, packed: nil,file_warning: :ignore)` 它无法识别file_url 作为 Excel 文件的路径。
两种方式:在控制器中执行或在模型中使用回调。
如果您只需要在用户从特定视图导入时执行此操作,则可以在控制器内处理。例如,如果在索引文件视图中上传 excel 文件调用 files_controller.rb 中的 'create' 操作,您可以执行以下操作:
if @file.save
# update bottle database using data from @file
end
但是,如果您希望每次创建新文件时都执行此操作,无论是否通过视图,您都可以添加一个
after_create
回调到您的 file.rb 模型。如果你希望每次保存后都完成,你可以做一个 after_save
回调。
好的 if@file.save
我加了Bottle.import(@file.file_url.path)
并且在 bottle.rb
模型中,我更改了
spreadsheet = Roo::Excel.new(file_url.path, packed: nil,file_warning: :ignore)
至
spreadsheet = Roo::Excel.new(file_url, packed: nil,file_warning: :ignore)
轰隆隆!
我有一个模型,file.rb
,一个控制器,filecontroller.rb
,以及一组名为 files
的 table 的视图。这样做是为了能够上传 Excel 文件,这样我就可以在索引视图中查看某人上传 Excel 文件的时间、创建时间和个人姓名的列表谁上传了 Excel 文件。
现在,我在名为 bottles
的数据库中有另一个模型 bottle.rb
、一个控制器 bottlecontroller.rb
和一组 table 的视图。数据库中 bottles table 中的列与 Excel 文件相同。
我已经能够在一些验证后上传 excel 文件,以便它们可以进入数据库中的 files
table。我还按照 rails cast 教程从瓶子索引视图中导入 excel 文件并更新我的 bottles
table。
但是,我希望能够上传 Excel 文件并在文件索引视图中导入信息。但是导入还需要更新 bottles 中的信息 table。所以换句话说,从文件索引视图导入的 Excel 需要能够更新 bottle table 数据库。
我知道我需要我的 files.rb
模型和 bottles.rb
模型之间的关联..但是任何帮助都会很棒!
************更新问题********************************* **********
在我的filecontroller.rb
中,我在保存后的创建方法中添加了@file.bottles = Bottle.import(params[:file_url])
。我将 excel 个文件存储在 :file_url
中。我还在 has_many :bottles
file.rb
模型中添加了一个关联
def create
.........
..........
if file.save
@file.bottles = Bottle.import(params[:file_url])
end
end
在我的瓶子模型中 bottle.rb
我有
def self.import(file_url)
spreadsheet = Roo::Excel.new(file_url.path, packed:nil,file_warning: :ignore)
.........
bottle.save!
end
但我得到一个未定义的方法 path' for nil:NilClas error in this line
spreadsheet = Roo::Excel.new(file_url.path, packed: nil,file_warning: :ignore)` 它无法识别file_url 作为 Excel 文件的路径。
两种方式:在控制器中执行或在模型中使用回调。
如果您只需要在用户从特定视图导入时执行此操作,则可以在控制器内处理。例如,如果在索引文件视图中上传 excel 文件调用 files_controller.rb 中的 'create' 操作,您可以执行以下操作:
if @file.save
# update bottle database using data from @file
end
但是,如果您希望每次创建新文件时都执行此操作,无论是否通过视图,您都可以添加一个
after_create
回调到您的 file.rb 模型。如果你希望每次保存后都完成,你可以做一个 after_save
回调。
好的 if@file.save
我加了Bottle.import(@file.file_url.path)
并且在 bottle.rb
模型中,我更改了
spreadsheet = Roo::Excel.new(file_url.path, packed: nil,file_warning: :ignore)
至
spreadsheet = Roo::Excel.new(file_url, packed: nil,file_warning: :ignore)
轰隆隆!