xxxbt 2011-10-20 11:31
浏览 328
已采纳

在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关联,同时能将数据存到其他相关的表格,麻烦了,刚刚接触这块,对数据库的知识也不是很懂!

  • 写回答

6条回答 默认 最新

  • horace_lee 2011-10-20 19:30
    关注

    老弟又是你啊,还是你的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的思路是帮你把问题简化。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面