Has_many and belongs_to association

On Thu, Jul 14, 2011 at 11:13 AM, Michael P. [email protected]
wrote:

so that i added it to language model

If you don’t want it, take it out.
Change the “name” field to “name_id” for all our sakes…

yes, I did change it

and it language table (DB)
it changed the name => name_id

in language model
belongs_to :name_id, :class_name => “Phrase”, :foreign_key =>
“name_id”

in phrase model
has_many :languages, :foreign_key => “name_id”

it complains

  1. Failure:
    test: belongs to and has many association should belong to phrase.
    (LanguageTest)
    [/usr/local/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:324]:
    Expected Language to have a belongs_to association called phrase (no
    association called phrase).
    Expected block to return true value.

  2. Failure:
    test: check has_many and belongs_to association should have many
    languages.
    (PhraseTest)
    [/usr/local/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:324]:
    Expected Phrase to have a has_many association called languages
    (Language
    does not have a name_id foreign key.).
    Expected block to return true value.

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

On 14 July 2011 17:26, joanne ta [email protected] wrote:

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?

On Thu, Jul 14, 2011 at 12:10 PM, Michael P. [email protected]
wrote:

On 14 July 2011 16:47, joanne ta [email protected] wrote:

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’,

/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

On 14 July 2011 19:55, joanne ta [email protected] wrote:

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.

On Thu, Jul 14, 2011 at 3:04 PM, Michael P. [email protected]
wrote:

Seems obvious to me… have you altered your table/migrations?
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,

It is there

On Thu, Jul 14, 2011 at 2:40 PM, Michael P. [email protected]
wrote:

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

On 14 July 2011 20:12, joanne ta [email protected] wrote:

It is there

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

On Thu, Jul 14, 2011 at 3:32 PM, Michael P. [email protected]
wrote:

“name_english”, “created_at”, “updated_at”, “id”, “name_id”) VALUES (
I am using the rails dbconsole to check the field in DB

On 14 July 2011 20:37, joanne ta [email protected] wrote:

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?

On 14 July 2011 20:46, joanne ta [email protected] wrote:

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

On Thu, Jul 14, 2011 at 3:41 PM, Michael P. [email protected]
wrote:

On 14 July 2011 20:37, joanne ta [email protected] wrote:

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

On Thu, Jul 14, 2011 at 4:10 PM, Michael P. [email protected]
wrote:

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.

On Thu, Jul 14, 2011 at 4:21 PM, Michael P. [email protected]
wrote:

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

On 14 July 2011 21:24, joanne ta [email protected] wrote:

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

On Thu, Jul 14, 2011 at 4:49 PM, Michael P. [email protected]
wrote:

no, I mean:
rake db:test:prepare

Yes, I did it too

On 14 July 2011 21:14, joanne ta [email protected] wrote:

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)