has_many and belongs_to association

Why would you change the association name to "name_id"? It's just the
foreign key to change:
   belongs_to :name, :class_name => "Phrase", :foreign_key => "name_id"

in language model

belongs_to :name_id, :class_name => “Phrase”, :foreign_key => “name_id”

Why would you change the association name to “name_id”? It’s just the

foreign key to change:

belongs_to :name, :class_name => "Phrase", :foreign_key => "name_id"

i changed it

now it gives me an error

Error:

test_validates_length_of_name(UserTest):

ActiveRecord::StatementInvalid: SQLite3::SQLException: table languages has no column named name_id: INSERT INTO “languages” (“name_english”, “name_id”, “created_at”, “updated_at”, “id”) VALUES (‘en’, ‘english’, ‘MyString’, 1, ‘f’, ‘MyString’, 1, ‘2011-07-14 16:22:20’, ‘2011-07-14 16:22:20’, 980190962)

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:239:in `insert_fixture’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:652:in `block in insert_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:588:in `each’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:588:in `insert_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:532:in `block (4 levels) in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:532:in `each’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:532:in `block (3 levels) in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:530:in `block (2 levels) in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:109:in `disable_referential_integrity’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:521:in `block in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/benchmarkable.rb:55:in `silence’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:520:in `create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:979:in `load_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:944:in `setup_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:415:in `_run_setup_callbacks’

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:34:in `run’

in yml file

Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:

name_english: english

name_id: 1

two:

name_english: french

name_id: 1

i changed it

now it gives me an error

Error:

test_validates_length_of_name(UserTest):

ActiveRecord::StatementInvalid: SQLite3::SQLException: table languages has no column named name_id: INSERT INTO “languages” (“name_english”, “name_id”, “created_at”, “updated_at”, “id”) VALUES (‘english’, 1, ‘2011-07-14 16:22:20’, ‘2011-07-14 16:22:20’, 980190962)

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:239:in `insert_fixture’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:652:in `block in insert_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:588:in `each’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:588:in `insert_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:532:in `block (4 levels) in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:532:in `each’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:532:in `block (3 levels) in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:530:in `block (2 levels) in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:109:in `disable_referential_integrity’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:521:in `block in create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/benchmarkable.rb:55:in `silence’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:520:in `create_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:979:in `load_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:944:in `setup_fixtures’

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:415:in `_run_setup_callbacks’

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:34:in `run’

in yml file

Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:

name_english: english

name_id: 1

two:

name_english: french

name_id: 1

i changed it

now it gives me an error

Have you read the error?

Error:

SQLException: table languages has no column named name_id

Seems obvious to me… have you altered your table/migrations?

i changed it

now it gives me an error

Have you read the error?

Error:

SQLException: table languages has no column named name_id

Seems obvious to me… have you altered your table/migrations?

no, i did not change anything from DB or table

I am really dont understand what the error is warned about

Since you took the advice to change the foreign key from “name” to “name_id”, you need to change the DB field to match. Alter your migration, and run it again, or create a new migration to rename the field.

Have you read the error?

Error:

SQLException: table languages has no column named name_id

Seems obvious to me… have you altered your table/migrations?

no, i did not change anything from DB or table

I am really dont understand what the error is warned about

Since you took the advice to change the foreign key from “name” to “name_id”, you need to change the DB field to match. Alter your migration, and run it again, or create a new migration to rename the field.

No, it does not work, :frowning: . it is an error of no column

table languages has no column named name_id: INSERT INTO “languages” “name_english”, “created_at”, “updated_at”, “id”, “name_id”) VALUES ( ‘english’, ‘2011-07-14 19:10:54’, ‘2011-07-14 19:10:54’, 980190962, 64810937)

It is there

I beg to differ - or at least Rails does. What have you done to check the field is in the DB?

Since you took the advice to change the foreign key from “name” to “name_id”, you need to change the DB field to match. Alter your migration, and run it again, or create a new migration to rename the field.

No, it does not work, :frowning: . it is an error of no column

table languages has no column named name_id: INSERT INTO “languages” “name_english”, “created_at”, “updated_at”, “id”, “name_id”) VALUES ( ‘english’, ‘2011-07-14 19:10:54’, ‘2011-07-14 19:10:54’, 980190962, 64810937)

It is there

I beg to differ - or at least Rails does. What have you done to check the field is in the DB?

I am using the rails dbconsole to check the field in DB

Out of curiosity, which rails dbconsole are you using? What command do you execute to get to this console?

I am using the rails dbconsole to check the field in DB

Out of curiosity, which rails dbconsole are you using? What command do you execute to get to this console?

I am using commad " rails dbconsole " to get to the db

and plus. I use the sqlite3 manager from firefox cho check too

Well something’s very odd then - because the SQL query that’s running is report that the languages table doesn’t have a name_id field, while you’re saying it does…

curious

I am using commad " rails dbconsole " to get to the db

and plus. I use the sqlite3 manager from firefox cho check too

Well something’s very odd then - because the SQL query that’s running is report that the languages table doesn’t have a name_id field, while you’re saying it does…

curious

me too… i am so curious what is happened when name_id is in DB

but it keeps saying no column :frowning:

thanks for the quick reply.

Hang on… this is breaking when you’re running tests? Have you raked the DB changes into your test DB (I’m assuming that the dbconsole is showing you the production DB)

me too… i am so curious what is happened when name_id is in DB

but it keeps saying no column :frowning:

Hang on… this is breaking when you’re running tests? Have you raked the DB changes into your test DB (I’m assuming that the dbconsole is showing you the production DB)

yes, I did rake db:create and db:migrate as well

no, I mean:

rake db:test:prepare

Hang on… this is breaking when you’re running tests? Have you raked the DB changes into your test DB (I’m assuming that the dbconsole is showing you the production DB)

yes, I did rake db:create and db:migrate as well

no, I mean:

rake db:test:prepare

Yes, I did it too