WebService的两种方式SOAP和REST比较 (转)

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:Style of WebService: REST vs. SOAP

[       在SOA的基础技术实现方式中WebService占据了很重要的地位,通常我们提到WebService第一想法就是SOAP消息在各种传输协议上交互。近几年REST的思想伴随着SOA逐渐被大

up vote 5 down vote favorite 2

My question is quite simple but I failed to find a clear answer.

I build a daily deals Rails app.

  • Each deal has many products (has_many)

  • Each product belongs to a deal

Folowing 2.3 from Rails Guides, I'll use this in my migration:

   class CreateDeal < ActiveRecord::Migration
    def change
      create_table :deals do |t|
        t.string :name
        t.timestamps
      end

      create_table :products do |t|
        t.belongs_to :Deal
        t.timestamps
      end
    end
   end

Automatically, Rails/active records will add in the Product Table a column deals_id right?

Do I need to add an index on this deals_id column manually (like below) by adding to my migration add_index or is it done "automatically" because of the belongs_to/has_many relationship I have set?

create_table :products do |t|
  t.belongs_to :Deal
  t.timestamps

  add_index :products, :deals_id 
end
ruby-on-rails ruby ruby-on-rails-3 activerecord share | improve this question edited Mar 22 '15 at 15:36 meagar ♦ 146k 22 223 255 asked Sep 7 '13 at 12:16 Mathieu 1,019 6 22 46      shouldn't t.belongs_to :Deal be t.belongs_to :deal –  Saad Masood Dec 10 '13 at 6:58 add a comment  | 

3 Answers 3

active oldest votes up vote 12 down vote accepted

You do need to add the index yourself... However, if you use the command line generator for the model and use belongs_to, Rails will add in the index into the migration...

e.g.

rails g model product deal:belongs_to

would produce

推荐:WebService的两种方式SOAP和REST比较

[转自:http://cenwenchu.javaeye.com/blog/316717我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑。如go

class CreateProducts < ActiveRecord::Migration
  def change
    create table :products do |t|
      t.belongs_to :deal

      t.timestamps
    end
    add_index :products, :deal_id
  end
end
share | improve this answer answered Sep 7 '13 at 14:10 Helios de Guerra 2,645 8 18 add a comment  |  up vote 2 down vote

You would need to add the indexing yourself.

Also your migration isn't quite right, you need a 3rd table ie:

class CreateDeal < ActiveRecord::Migration
  def change
    create_table :deals do |t|
      t.string :name
      t.timestamps
    end

    create_table :products do |t|
      t.string :title
      t.timestamps
    end

    create_table :deals_products do |t|
      t.belongs_to :deal
      t.belongs_to :product
    end
  end
end

As per http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

share | improve this answer answered Sep 7 '13 at 12:38 veritas1 3,011 2 10 28      This is the setup for a many-to-many relationship, not the simple has_many relationship Mathieu is talking about. I would personally avoid this setup unless you really need a many-to-many association because with this set up you have 3 tables requiring 2 indexes (which were left out of this migration) instead of 2 tables and one index. As I understand it, while index speed up queries, they make save/update processes take longer so you're generally better off minimizing their use to only when you need them to reduce your overhead. In this case, it seems that one index should suffice. –  makewavesnotwar Sep 7 '13 at 14:39      Yeah my mistake - I don't know where I got that he was using many-to-many from. –  veritas1 Sep 7 '13 at 16:01 add a comment  |  up vote 1 down vote

Mathieu, in situations of doubt like this, where you're not sure if something is being created or not created: it seems best to just explicitly create what you think is needed (in this case the index) and see what happens when you run the migration.

The logic behind this is that if your :deal_id column is already indexed and your migration tries to re-index it, it will get an error and the migration will rollback so you can fix it. However if you didn't add the index in your migration, you would obviously not get any errors but you'd have to take an extra step to check if the index exists.

class CreateDeal < ActiveRecord::Migration
  def change
    create_table :deals do |t|
      t.string :name
      t.timestamps
    end

    create_table :products do |t|
      t.belongs_to :Deal
      t.timestamps
    end

    add_index :products, :deal_id
  end
end

Note that you also want to add the index after the table creation process has complete. Using the add_index helper inside the create_table helper is probably going to cause an error.

share | improve this answer answered Sep 7 '13 at 15:08 makewavesnotwar 395 4 10 add a comment  | 

Your Answer

  draft saved draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name Email

Post as a guest

Name Email discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged ruby-on-rails ruby ruby-on-rails-3 activerecord or ask your own question.

推荐:REST架构与SOAP WebService的比较

[    一般客户端访问服务器端web服务通常可以由HTTPService、WebService、RemoteObject等方式来实现。通常实现web服务我们最容易想到的是SOAP协议的WebService,这 ...]

相关推荐