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

On 11 July 2011 17:05, Yennie [email protected] wrote:

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

On Jul 11, 12:13pm, Colin L. [email protected] wrote:

Hi ,
in language.rb
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

plz help … thanks

On 11 Jul 2011, at 17:59, Yennie [email protected] wrote:

should have_many :phrases
shoulda-2.11.3/lib/shoulda/context.rb:324]:

belongs_to :category, :class_name => “Phrase”

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

On Jul 11, 2:59pm, Frederick C. [email protected]
wrote:

in my test
test: checkhas_manyandbelongs_toassociationshould have many
belongs_to:name.
“category_id”
end
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

On Tue, Jul 12, 2011 at 3:01 PM, Tom [email protected] wrote:

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”

On 11 July 2011 17:59, Yennie [email protected] wrote:

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

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

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

On Wed, Jul 13, 2011 at 4:25 PM, Colin L. [email protected]
wrote:

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

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

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

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

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

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”

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

directions
language_name

PS Where’s your phrase_id in the language table?

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

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?

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

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?

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

directions
language_name
~phrase model
class Phrase < ActiveRecord::Base
has_many :languages
end
~ phrase table
id
name
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”.

On 14 July 2011 15:41, Michael P. [email protected] wrote:

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 :-/

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

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.

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

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

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

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”