has_many and belongs_to association

Hi ,

I want to test the one below but I got the problem

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

in my test
context "test"do
   should have_many :phrases
end

in language.rb
  belongs_to :name, :class_name => "Phrase", :foreign_key => "name"

error is

  1) 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 phrase_id foreign key.).
Expected block to return true value.

I dont really understand what it's warning now..

Yennie

It might help us if you showed us the failing test. Also I think it
would be better to use name_id as the foreign key for language
belongs_to :name.

Colin

model language.rb
class Language < ActiveRecord::Base
belongs_to :phraes, :class_name => "Phrase", :foreign_key => "name"
end

model phrase.rb
has_many :phrases, :class_name => "Phrase", :foreign_key =>
"category_id"
  belongs_to :category, :class_name => "Phrase"

  has_many :languages
end

test file phrase_test.rb
context "check has_many and belongs_to association" do
   should belong_to :category
   should have_many :phrases
   should have_many :languages
end

error

Started
..........F.......................................
Finished in 1.313199 seconds.

  1) 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 phrase_id foreign key.).
Expected block to return true value

plz help .. thanks

Hi ,

I want to test the one below but I got the problem

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

in my test
context "test"do
  should have_many :phrases
end

in language.rb
belongs_to :name, :class_name => "Phrase", :foreign_key => "name"

error is

1) 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 phrase_id foreign key.).
Expected block to return true value.

I dont really understand what it's warning now..

It might help us if you showed us the failing test. Also I think it
would be better to use name_id as the foreign key for language
belongs_to :name.

Colin

model language.rb
class Language < ActiveRecord::Base
belongs_to :phraes, :class_name => "Phrase", :foreign_key => "name"
end

model phrase.rb
has_many :phrases, :class_name => "Phrase", :foreign_key =>
"category_id"
belongs_to :category, :class_name => "Phrase"

has_many :languages
end

test file phrase_test.rb
context "check has_many and belongs_to association" do
  should belong_to :category
  should have_many :phrases
  should have_many :languages
end

error

Started
..........F.......................................
Finished in 1.313199 seconds.

1) 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 phrase_id foreign key.).
Expected block to return true value

The test claims to be failing because the languages table does not have a phrase_id column. Does it?

Fred

>>> Hi ,

>>> I want to test the one below but I got the problem

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

>>> in my test
>>> context "test"do
>>> should have_many :phrases
>>> end

>>> in language.rb
>>> belongs_to:name, :class_name => "Phrase", :foreign_key => "name"

>>> error is

>>> 1) Failure:
>>> test: checkhas_manyandbelongs_to associationshould 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 ahas_manyassociationcalled languages
>>> (Language does not have a phrase_id foreign key.).
>>> Expected block to return true value.

>>> I dont really understand what it's warning now..

>> It might help us if you showed us the failing test. Also I think it
>> would be better to use name_id as the foreign key for language
>>belongs_to:name.

>> Colin

> model language.rb
> class Language < ActiveRecord::Base
>belongs_to:phraes, :class_name => "Phrase", :foreign_key => "name"
> end

> model phrase.rb
>has_many:phrases, :class_name => "Phrase", :foreign_key =>
> "category_id"
> belongs_to:category, :class_name => "Phrase"

> has_many:languages
> end

> test file phrase_test.rb
> context "checkhas_manyandbelongs_to association" do
> should belong_to :category
> should have_many :phrases
> should have_many :languages
> end

> error

> Started
> ..........F.......................................
> Finished in 1.313199 seconds.

> 1) Failure:
> test: checkhas_manyandbelongs_to associationshould 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 ahas_manyassociationcalled languages
> (Language does not have a phrase_id foreign key.).
> Expected block to return true value

The test claims to be failing because the languages table does not have a phrase_id column. Does it?

Fred

I have language table
id
name (integer)
language_name (varchar)

phrase table
id
name (varchar)
category_id (integer)

I have name as references to phrase..
can anyone help me plzz??

model language.rb
class Language < ActiveRecord::Base
belongs_to :phraes, :class_name => "Phrase", :foreign_key => "name"
end

Id phrase misspelled?
T

> Hi ,

> I want to test the one below but I got the problem

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

> in my test
> context "test"do
> should have_many :phrases
> end

> in language.rb
> belongs_to :name, :class_name => "Phrase", :foreign_key => "name"

> error is

> 1) 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 phrase_id foreign key.).
> Expected block to return true value.

> I dont really understand what it's warning now..

It might help us if you showed us the failing test. Also I think it
would be better to use name_id as the foreign key for language
belongs_to :name.

Colin

model language.rb
class Language < ActiveRecord::Base
belongs_to :phraes, :class_name => "Phrase", :foreign_key => "name"

You have misspelt phrase (you have phraes).

end

model phrase.rb
has_many :phrases, :class_name => "Phrase", :foreign_key =>
"category_id"
belongs_to :category, :class_name => "Phrase"

has_many :languages

I *think* you have to specify the foreign key here also so that the
phrase model knows how to find the associated languages.

model language.rb

class Language < ActiveRecord::Base

belongs_to :phraes, :class_name => “Phrase”, :foreign_key => “name”

end

I did fix the spelling already

belongs_to :phrase, :class_name => “Phrase”, :foreign_key => “name”

So since you have not said I assume it is all now working?

If not did you try including the foreign key with has_many :languages,
as I suggested?

If it is still not working please re-post the code and error exactly
as it is now (copy/paste, do not re-type it).

Colin

model language.rb

class Language < ActiveRecord::Base

belongs_to :phraes, :class_name => “Phrase”, :foreign_key => “name”

end

I did fix the spelling already

belongs_to :phrase, :class_name => “Phrase”, :foreign_key => “name”

So since you have not said I assume it is all now working?

If not did you try including the foreign key with has_many :languages,

as I suggested?

If it is still not working please re-post the code and error exactly

as it is now (copy/paste, do not re-type it).

Colin

It does work at all. it just keeps giving me the same warning …

~language model

class Language < ActiveRecord::Base

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

belongs_to :phrase

has_many :users

end

~ language table

id

name

directions

language_name

~phrase model

class Phrase < ActiveRecord::Base

has_many :languages

end

~ phrase table

id

name

category

  1. 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 phrase_id foreign key.).

Expected block to return true value.

please help and thanks

Why is the foreign key "name"? Is the "name" field in language a text
field, or an integer? It needs to be an integer to store the id of the
Phrase, and would make more sense as "name_id".

It does work at all. it just keeps giving me the same warning …

~language model

class Language < ActiveRecord::Base

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

belongs_to :phrase

has_many :users

end

~ language table

id

name

is integer

directions

language_name

~phrase model

class Phrase < ActiveRecord::Base

has_many :languages

end

~ phrase table

id

name

this is a string

category

Why is the foreign key “name”? Is the “name” field in language a text

field, or an integer? It needs to be an integer to store the id of the

Phrase, and would make more sense as “name_id”.

name in language is integer (is foreign key of phrase)

name in phrase is string

please help

By having the relationship named the same as an attribute, that's
going to confuse things, because when something calls "language.name",
is it expecting the phrase object back, or the integer value of the
name field?

Change the foreign key field to "name_id"

PS Where's your phrase_id in the language table?

class Language < ActiveRecord::Base

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

belongs_to :phrase

has_many :users

end

~ language table

id

name

is integer

directions

language_name

PS Where’s your phrase_id in the language table?

there is not phrase_id in language model

i use the name (phrase 's foreign key) …

is there any syntax mistakes?

In Language, you have "belongs_to :name" (which is really Phrase), and
belongs_to :phrase. So you need a phrase_id field, and a name_id field
- one for each of the relationships.

In Phrase, you "has_many :languages", but language doesn't yet have a
phrase_id field. Or did you mean for this has_many to point at the
name_id field as foreign key?

I've just read through this thread: I'm sorry for repeating stuff you've
already been told by two different people :-/

class Language < ActiveRecord::Base

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

belongs_to :phrase

has_many :users

end

~ language table

id

name

is integer

directions

language_name

PS Where’s your phrase_id in the language table?

there is not phrase_id in language model

i use the name (phrase 's foreign key) …

is there any syntax mistakes?

In Language, you have “belongs_to :name” (which is really Phrase), and

belongs_to :phrase. So you need a phrase_id field, and a name_id field

  • one for each of the relationships.

belongs_to phrase, I add it because the languageTest is complaining there is no association with phrase.

so that i added it to language model

In Phrase, you “has_many :languages”, but language doesn’t yet have a

phrase_id field. Or did you mean for this has_many to point at the

name_id field as foreign key?

yes. I want to point the name_id as foreign key of phrase

one phrase(id) can have many language (name_id/phrase_id)

I have so confused to have this warning…

please help and thanks for the quick reply.

In Language, you have "belongs_to :name" (which is really Phrase), and
belongs_to :phrase. So you need a phrase_id field, and a name_id field
- one for each of the relationships.

belongs_to phrase, I add it because the languageTest is complaining there is
no association with phrase.
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....

In Phrase, you "has_many :languages", but language doesn't yet have a
phrase_id field. Or did you mean for this has_many to point at the
name_id field as foreign key?

yes. I want to point the name_id as foreign key of phrase
one phrase(id) can have many language (name_id/phrase_id)

So do that then (how's Rails supposed to know that you've decided for
no reason not to use "phrase_id" as the foreign key field in
Language?!):
  has_many :languages, :foreign_key => :name_id

In Language, you have “belongs_to :name” (which is really Phrase), and

belongs_to :phrase. So you need a phrase_id field, and a name_id field

  • one for each of the relationships.

belongs_to phrase, I add it because the languageTest is complaining there is

no association with phrase.

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.

  1. 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.