Reblog a post when user repost from another user timeline - ruby-on-rails

I need to enable a functionality like tumblr or similar blogs, where the the user can see the original poster (creator) and the user who reposted.
e.g. User A create a post1, User B 'repost' post1, User C discovered post1, because he follow user B or whatever. So user C must see 'post1, posted by User A, reposted by UserB... and so on if user C repost post1.
Currently user can like a micropost, but I also need to be able to 're_like' a micropost.
Firs I need to know the post belongs to reposter user.
I've seen some approach where we have different urls depending on user who reposted.
original ./post/1/
reposted ./post/1/reposted/18 (I guess 18 is repost_id or reposter_id)
My Models:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :likes
has_many :liked_posts, through: :likes, source: :micropost, dependent: :destroy
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :micropost
has_many :likes
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :likes
has_many :liking_users, through: :likes, source: :user, dependent: :destroy
Rails 4.2.0

Related

Retrieving all posts from a board that a user's subscribed to

I'm currently setting up a feed from scratch where a user subscribes to multiple message boards and would receive all of the posts created under that board. This is how I have my code written so far:
class User < ApplicationRecord
has_many :subscriptions, dependent: :destroy
has_many :boards, through: :subscriptions
end
class Subscription < ApplicationRecord
belongs_to :user
belongs_to :board
end
class Board < ApplicationRecord
has_many :subscriptions, dependent: :destroy
has_many :subscribers, through: :subscriptions, source: :user
end
class Post < ApplicationRecord
belongs_to :board
belongs_to :user
end
When it comes time to display the posts is where I'm having problems as far as the proper approach. Would the simple solution be to setup another HMT association in the user model as so?
has_many :subscribed_posts, through: :boards, source: :posts
One immediate flaw I see in my approach is that the user would get their posts to show up including those of other users. Would the better solution be to create some sort of SQL query?
Yes, you can build another association to retrieve posts you need, but as you mentioned that would return also the users' posts too, so you have to put some conditions to prevent such behavior.
has_many :subscribed_posts, through: :boards, source: :posts, conditions: {["user_id != ?", id] }

Additional relation between records in Rails 5.0

I need your small advice help.
I have trivial models: Post and User.
A user has_many :posts and a post belongs_to :user. A post has one owner.
I need somehow add an additional relation: a post can have multiple contributors. A contributor is a user too. I need to be able to write something like this: #post.contributors (shows User records) and #user.contributed_to (shows Post records).
How do I do that?
You'll need many-to-many association here, because user has_many posts and posts has_many users.
To implement it, you need to create an additional model (for example, contribution with user_id and post_id columns)
class Contribution < ApplicationRecord
belongs_to :user
belongs_to :post
end
And your Post and User classes will contain something like this:
class Post
belongs_to :user
has_many :contributions
has_many :contributors, through: :contributions, source: :user
end
class User
has_many :posts
has_many :contributions
has_many :contributed_posts, through: :contributions, source: :post
end

Active Record association has_many with multiple foreign keys

I'm trying to set up a referral model. A referral contains a user who is referred, a user who does the referring, and a doctor
class User < ActiveRecord::Base
has_many :referrals
belongs_to :profile, polymorphic: true
end
class Referral < ActiveRecord::Base
belongs_to :user
belongs_to :referrer, :class_name => "User"
belongs_to :doctor, :class_name => "User"
end
I'm able to create the generic has_many :referrals to see doctors that have been referred to the user however I'd also like to see the doctors that you've referred to others (using the referrer column).
I've tried has_many :doctors_referred, primary_key: "referrer_id" and has_many :doctors_referred, through: :referrals ,source: "referrer" with no luck. How can I see which doctors a user has referred?
# mostly irrelevant
class PatientProfile < ActiveRecord::Base
has_one :user, as: :profile
end
class DoctorProfile < ActiveRecord::Base
has_one :user, as: :profile
end
My first attempt would be this:
class User < ActiveRecord::Base
has_many :referrals_as_referrer, source: :referred
has_many :referrals_as_referree, source: :user
has_many :doctors_as_referrer, through: :referrals_as_referrer
has_many :doctors_as_referree, through: :referrals_as_referree
end
The issue here is that saying has_many :referrals isn't enough because you can have a referral where you were the referrer or one where you were the referree. With this you can type user.referrals_as_referrer which will give a list of referrals where user is the referrer. You can also write user.doctors_as_referrer which will go through the previous association and retrieve a list of users that represent doctors which were referred by a referral in the user.referrals_as_referrer list.
I haven't tested this and I might be wrong, but let's iterate once you try it out.

Has many association on tagging user in a post

In my app a User can 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 :posts
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
has_many :tags
class Tag < ActiveRecord::Base
belongs_to :tagger_user
belongs_to :tagged_user
belongs_to :post
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 has_many :through
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.

Rails: ignoring duplicates in an nested association

I have models User, Team, Document. There's a many-to-many relationship between Users and Teams, and a many-to-many relationship between Teams and Documents, using join tables called TeamMembership and TeamDocument respectively.
The relationships in my models look like this:
class Document < ActiveRecord::Base
has_many :team_documents, dependent: :destroy
has_many :teams, through: :team_documents
end
class User < ActiveRecord::Base
has_many :team_memberships, dependent: :destroy, foreign_key: :member_id
has_many :teams, through: :team_memberships
has_many :documents, through: :teams
end
class TeamDocument < ActiveRecord::Base
belongs_to :team
belongs_to :document
end
class TeamMembership < ActiveRecord::Base
belongs_to :team
belongs_to :member, class_name: "User"
end
class Team < ActiveRecord::Base
has_many :team_documents, dependent: :destroy
has_many :documents, through: :team_documents
has_many :team_memberships, dependent: :destroy
has_many :members, through: :team_memberships
end
The idea is that users can belong to many teams, a document can be associated with many teams, and users will only have access to documents that "belong" to at least one team that the user is a member of.
Here's the question: I can use User#documents to retrieve a list of all the documents that this user is allowed to view. But this will return duplicates if a document is viewable by more than one team which the user is a member of. How can I avoid this?
I know I can remove the duplicates after the fact with #user.documents.uniq, but as I will never want to include the duplicates in any case, is there a way I can just make #documents not include duplicates every time?
I don't have nested has_many :through like yours to test it, but I suspect using uniq option on your user association would help :
class User < ActiveRecord::Base
has_many :documents, through: :teams, uniq: true
end
You can add a default_scope on Document model:
class Document < ActiveRecord::Base
default_scope group: { documents: :id }

Resources