I'm looking to make sure my methods are correct before pursuing this association. The implementation sounds too complicated, so I think there must be something wrong with my plan. I am using structured (SQL) data storage, conforming to the rails storage conventions. What I have is a User model, it has an email address, password_digest
, and name in the Schema.
class User < ActiveRecord::Base
has_many :posts
end
I'd like to implement a has_many
association to a friends collection, so that Users can belong_to
Users (as friends). I'm hoping to be able to have User.last.friends.last
return a User object when properly built and populated.
I believe I can create a model for this association like:
Class Friend < ActiveRecord::Base
belongs_to :user
belongs_to :friendlies, class: 'User'
end
Class User < ActiveRecord::Base
has_many :posts
has_many :friends
has_many :friendly, class: 'Friend'
end
But I think that will require me to add an as to the models and query using User.last.friends.last.user
So what I was thinking is this is a has_and_belongs_to_many
relationship. Can I get away with the following (or something similar):
class User < ActiveRecord::Base
has_and_belongs_to_many :friends, class: 'User'
end
I found this:
class User < ActiveRecord::Base
has_many :user_friendships
has_many :friends, through: :user_friendships
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
And this(self proclaimed 'standard'):
has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :dependent => :destroy
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id", :dependent => :destroy
has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :dependent => :destroy
Which I assume requires a Friendship
model. I don't feel like I need a friendship model. I think that the class UserFriendship
method looks right, but it requires an additional model. Now onto the questions:
Can I do a
has_and_belongs_to_many
relationship with a table that relates user to friends which are users, without incurring an additional model to do so?Is it prudent to have an additional model 'in case' additional requirements pop up later?