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
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.
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.
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.
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
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??
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
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.
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
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?
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?
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 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…
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
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”