0

I am trying to create a "one-to-many relationship between owner and property" but I don't know why I am getting this error, after generating a model I tried to add_index before run rake db:migrate but something went wrong Note: It's the first time to create a relation between two tables using ruby on rails and I searched in rubyonrails guide put I did't find an answer

My migration file:

class CreateProperties < ActiveRecord::Migration
  def change
    create_table :properties do |t|
      t.string :p_street
      t.string :p_city
      t.string :postcode
      t.string :type
      t.integer :rooms
      t.integer :rent
      t.integer :owner_id

      t.timestamps
    end
    add_index :owner_id
  end
end

The error that appears:

==  CreateProperties: migrating ===============================================
-- create_table(:properties)
   -> 0.0712s
-- add_index(:owner_id)
rake aborted!
An error has occurred, this and all later migrations canceled:

wrong number of arguments (1 for 2)/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/schema_statements.rb:350:in `add_ind
ex'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:466:in `block in method_missing'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `block in say_with_time'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `say_with_time'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:458:in `method_missing'
/root/rails_project/dream_home/db/migrate/20130503044654_create_properties.rb:14:in `change'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:407:in `block in migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:389:in `migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:528:in `migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `call'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `ddl_transaction'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:719:in `block in migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `each'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:570:in `up'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/migration.rb:551:in `migrate'
/var/lib/gems/1.9.2/gems/activerecord-3.2.13/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/var/lib/gems/1.9.2/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

2 Answers 2

2

You have to mention table name in the arguments. Try add_index (:properties,:owner_id)

Sign up to request clarification or add additional context in comments.

2 Comments

I know but I am a new comer here and I don't have enough reputation :(
Its ok. Anyways I love to help.
0

add_index needs a table name

add_index :properties, :owner_id

3 Comments

In properties.rb I should add "belongs_to :owner" and in owners.rb I should add "has_many :properties",and thats all Am I right or there is a something need to be add in controllers ?
Yes, then you would be able to do something like Owner.find(params[:id]).properties to get all of their properties.
thanks :) I will add the relation and then try something to do :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.