在rails中向多张关联的表中插入数据的问题。

我现在要讲一个服务注册到数据库中去,先简单说一下情况

因为一个服务有具体的名称,还有不同的服务类型(其实这里有两种类型,比如下面的soap_services,还有一个是rest_services,注册的时候可以选择注册的类型,然后跳转到具体的类型页面,提交相应的数据,这里提交的是XML的文档,我将它进行了解析,但是不知道如何完整的存到数据库里面去),同时还有若干个操作,每个操作又有若个个参数

我现在要用四张表来存储,第一个表里面只存服务的名称,第二个表里面存服务名称和描述,第三个表里面存储服务包含的操作名称,描述,第四个表里面存服务某一个具体操作的参数和描述(最后的两个表4_1和 4_2是并列的,因为每个服务的输入输出个数不一样,一张表无法解决)

具体的表是这样设计的

第一张表
[code="java"]
class CreateServices < ActiveRecord::Migration
def self.up
create_table :services do |t|
t.column :name, :string
t.column :user_id, :integer
t.column :updated_at, :datetime
t.column :created_at, :datetime
end
end

def self.down
drop_table :services
end
end
[/code]
第2张表
[code="java"]
class CreateSoapServices < ActiveRecord::Migration
def self.up
create_table :soap_services do |t|
t.column :name, :string
t.column :wsdl_location, :string
t.column :namespace, :string
t.column :description, :text
t.column :updated_at, :datetime
t.column :created_at, :datetime
end
end

def self.down
drop_table :soap_services
end
end
[/code]

第3张表
[code="java"]
class CreateSoapOperations < ActiveRecord::Migration
def self.up
create_table :soap_operations do |t|
t.column :name, :string
t.column :description, :text
t.column :soap_service_id, :integer
t.column :parameter_order, :string
t.column :parent_port_type, :string
t.column :soap_service_port_id, :integer
t.column :updated_at, :datetime
t.column :created_at, :datetime
end
end

def self.down
drop_table :soap_operations
end
end
[/code]
第4_1张表
[code="java"]
class CreateSoapInputs < ActiveRecord::Migration
def self.up
create_table :soap_inputs do |t|
t.column :name, :string
t.column :description, :text
t.column :soap_operation_id, :integer
t.column :computational_type, :string
t.column :computational_type_details, :text, :limit => 2.megabytes
t.column :min_occurs, :integer
t.column :max_occurs, :integer
t.column :updated_at, :datetime
t.column :created_at, :datetime
end
end

def self.down
drop_table :soap_inputs
end
end
[/code]
第4_2张表
[code="java"]
class CreateSoapOutputs < ActiveRecord::Migration
def self.up
create_table :soap_outputs do |t|
t.column :name, :string
t.column :description, :text
t.column :soap_operation_id, :integer
t.column :computational_type, :string
t.column :computational_type_details, :text, :limit => 2.megabytes
t.column :min_occurs, :integer
t.column :max_occurs, :integer
t.column :updated_at, :datetime
t.column :created_at, :datetime
end
end

def self.down
drop_table :soap_outputs
end
end
[/code]

提交的数据是在soap_services 的new页面中,如何能先将服务的名称存在第一张表里面,然后根据第二张表根据第一个表存储的id关联,同时能将数据存到其他相关的表格,麻烦了,刚刚接触这块,对数据库的知识也不是很懂!

老弟又是你啊,还是你的soap service项目哈,
那我简单给你提个醒吧

  1. 数据库表设计
    这个你要想清楚,

    比如,
    Services表和SoapServices表,是怎么关联的?(不应该是有相同的name吧?)
    Serivices表和SoapServices表是什么关系,一对一,一对多?

    其他,SoapOperations,SoapInputs,SoapOutputs也是一样,先梳理清楚

  2. 我先假设一个表关系吧。

Services 和SoapServices一对一
SoapServices和SoapOperations一对多
SoapOperations和SoapInputs,SoapOutputs分别一对多

那吗,你要首先在model里体现出来,用has_one, has_many, belongs_to

道理上讲,这时你真正存储services信息的时候就可以存了,当然,也可以用回调了。
比如,你表单提交了有关service信息

[code="ruby"]@service = Service.new(params[:service])[/code]

你要同时保存其他信息,就可以用
[code="ruby"]@soap_service = @service.soap_service.new(params)[/code]
也会有
[code="ruby"]
@service.soap_service.soap_operations.first.soap_inputs
[/code]之类吧。有关系啦,当然想保存就在保存service信息时候,把其他信息保存就行了。

  1. 当然,这里实际还有一个问题,就是嵌套表单。 因为你的表是多级嵌套的(我这里希望你不是过度设计,因为,我要是刚开始一个项目,是不会一下把表设计的很复杂,比如,我会先一个表搞定,里面啥信息都存,大不了冗余,过后,什么地方有问题,重构什么地方),所以,提交表单信息的时候,就涉及一个嵌套表单的问题,说起来也不是复杂的东西啦,railscast上有上下两集讲怎么用的,看看就好,给你个链接哈

[url]http://cn.asciicasts.com/episodes/196-nested-model-form-part-1[/url]
[url]http://cn.asciicasts.com/episodes/197-nested-model-form-part-2[/url]

  1. 数据能提交上来就要考虑scope了,因为关联比较复杂的表,一定涉及查询怎么写的问题。当然,那是以后的事,先不提。

这种情况考虑回调也是正常的想法,楼上提啦
实际我也就简单一提,重点的部分还得看你,rails的思路是帮你把问题简化。

可以写一个call back啊,第一张表存好后用call back存第二张表

在第一张表对应的model里面写一个
after_save :next_to_do # 这里是你要做操作的方法名啊

def next_to_do # 这里是要做的操作方法

# 在这里把要对下一张表做的操作写下来
# 比如把本张表的name作为下一张表的name
SoapService.create :name => self.name

end

在下一个model就是SoapService里面同样这样写
..........

总之逻辑可以写在after_save或者after_create所调用的方法里面,具体回调方法里面的逻辑这个真要自己写了

每一个model在save之后用self.id可以取到自己的数据库id,当然用build也可以

详细的东西可以去[url]http://guides.rubyonrails.org[/url]看一看关于callback这个部分