In my app a
Post something to his timeline and
Tag other's users in his post (something like facebook). In my model I have to know who was tagged in which post and who did tag (always the post's owner). I've found something similar here with hashtags model, but I couldn't get the expected outcome with this.
My first question is: the approach below is right? If so, how could I validate the existence of the user before the association is created?
class User < ActiveRecord::Base
has_many :tagger_users, class_name: "Tag", foreign_key: "tagger_id"
has_many :tagged_users, class_name: "Tag", foreign_key: "tagged_id"
class Post < ActiveRecord::Base
class Tag < ActiveRecord::Base
validates_presence_of :tagger_user, :tagged_user # This always return ["Tagger post can't be blank", "Tagged post can't be blank"] even when the users are correctly set
I think the correct thing would be having a
Tag which belongs to two users, the tagged and the tag. Then the post can have a owner, and a list of tagged users using
class User < ActiveRecord::Base has_many :posts class Post < ActiveRecord::Base has_many :users, :through: :tags belongs_to :user class Tag < ActiveRecord::Base belongs_to :tagger_user, class_name: user belongs_to :tagged_user, class_name: :user validates_presence_of :tagger_user validates_presence_of :tagged_user
About validating check this answer. If the ends are valid, they should be automatically persisted.