Tag: 自我加入

如何在ActiveRecord中进行自反的自我加入关系?

我正在尝试实现一个社交网络风格的友谊模型,我没有太多的运气试图找出那里可用的插件。 如果我自己做的话,我想我会更好地学习Rails。 所以这就是我所拥有的: class User :friend_id, :class_name => ‘Friendship’ has_many :inviter_friends, :through => :invitee_friendships has_many :inviter_friendships , :foreign_key => :user_id, :class_name => ‘Friendship’ has_many :invited_friends, :through => :inviter_friendships end class Friendship < ActiveRecord::Base belongs_to :user //I think something needs to come here, i dont know what end 在我尝试这个时: friend1 = Friend.create(:name => ‘Jack’) friend2 = […]

Controller中的ActiveRecord :: AssociationTypeMismatch#在下拉列表中创建Rails自联接

我在Rails 5中自我加入时遇到ActiveRecord :: AssociationTypeMismatch错误,我无法弄清楚如何修复。 这是一个简单的rails应用程序,用户可以分享艺术家(如David Bowie)关于另一位艺术家(如Lou Reed)的引用。 所以,引用可能如下所示: 引用主题:David Bowie内容:“他是一位大师。” 演讲者:Lou Reed 我有一个Quote模型和一个Artist模型,主题和演讲者被定义为Artist模型上的自联接。 以下是模型: class Artist { order(name: :asc) } belongs_to :user has_many :spoken_quotes, class_name: “Quote”, foreign_key: :speaker_id has_many :topic_quotes, class_name: “Quote”, foreign_key: :topic_id validates :user_id, presence: true validates :name, presence: true, length: { maximum: 60 }, uniqueness: { case_sensitive: false } end class Quote […]