I recently updated this application from rails 2.2.2 to 2.3.11. Everything was running fine before the upgrade. After the upgrade i am getting the following error:
ActiveRecord::HasAndBelongsToManyAssociationForeignKeyNeeded in InstrumentsController#arrow
Cannot create self referential has_and_belongs_to_many association on 'Trait#traits'. :association_foreign_key cannot be the same as the :foreign_key.
In Gift model:
class Gift < ActiveRecord::Base
has_many :delegate_gifts
has_many :answers
belongs_to :feel_motive, :class_name => "Trait", :foreign_key => "feel_motive_id"
belongs_to :see_motive, :class_name => "Trait", :foreign_key => "see_motive_id"
belongs_to :incline_motive, :class_name => "Trait", :foreign_key => "incline_motive_id"
has_and_belongs_to_many :users
has_and_belongs_to_many :best_contributions
def traits
traits = []
traits << feel_motive unless feel_motive.nil?
traits << see_motive unless see_motive.nil?
traits << incline_motive unless incline_motive.nil?
return traits
end
end
trait model:
class Trait < Field
has_and_belongs_to_many :traits
end
Why does upgrading from 2.2.2 to 2.3.11 produce this error?
Description: Microsoft Access can’t create this relationship and enforce referential integrity.@Data in the table violates referential integrity rules. For example, there may be records relating to an employee in the related table, but no record for the empl What Is Relationship In Access Database?
Self reference for fields inside resource definition is hard, because it may produce infinite loop - ae6bf24 #diff-2437af93acd4d73bd3a7a3fb1d4a38b6R9 But self.resource_name is safe from infinite loops. Fixing it requires setting it somewhere, and lifting limitation on self reference for this specific case in
Referential integrity is a standard that means any CUSTOMER_ID value in the CUSTOMER_MASTER table can’t be edited without editing the corresponding value in the ACCOUNTS_MASTER table. What Is The Purpose Of Enforcing Referential Integrity?
It shouldn't be just the resource name. All non-dynamic attributes should be referenceable. Sorry, something went wrong. Thanks for this feature request @abitrolly, and sorry for the delay in responding.
has_and_belongs_to_many
can not point to itself (at least not in the easy way). That is why you have "self referential" error. If you really need this recurrent association then you have to write something like this:
class User < ActiveRecord::Base
has_and_belongs_to_many :friends,
:class_name => "User",
:association_foreign_key => "friend_id",
:join_table => "friends_users"
end
so you need additional field friend_id
in users table and new join table friends_users with fields: user_id
and friend_id
Note: more information you can find there: http://railsforum.com/viewtopic.php?id=4237)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With