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?
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
create_table :products do |t| t.belongs_to :Deal t.timestamps add_index :products, :deals_id end
t.belongs_to :deal– Saad Masood Dec 10 '13 at 6:58 add a comment |
3 Answers 3active 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...
rails g model product deal:belongs_to
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
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
You would need to add the indexing yourself.
Also your migration isn't quite right, you need a 3rd table ie:
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
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
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 Answerdraft 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 guestName Email
Post as a guestName Email discard
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.
[ 一般客户端访问服务器端web服务通常可以由HTTPService、WebService、RemoteObject等方式来实现。通常实现web服务我们最容易想到的是SOAP协议的WebService，这 ...]