Morgan

Joined

1,750 Experience
6 Lessons Completed
1 Question Solved

Activity

Posted in Looking for advice on how to setup a relationship.

Thanks for the detailed reply Jacob!

Just to be clear, Article and Review are content models that belong to a User and don't necessarily have to be shared via Post which means they can exist independently without being associated to a Post.

Basically, all the Post model does is post and schedule content to various sites so it might look like this:

User creates a:
Video -> Post -> YouTube
Article -> Post ->Facebook feed
Review --> Post -> Twitter, Facebook & YouTube
SomeOtherModel -> Post -> Some other network

The reason I'm using a separate (perhaps confusingly named) Post model is that it also handles scheduling via background jobs so it has some extra fields to track errors and status etc.

Would your suggestion still apply in this case?

Posted in Looking for advice on how to setup a relationship.

I'm trying to figure out the best way to setup relationships between models which allows users to publish saved content from multiple models.

ExampIe:

I have a Post model which allows users to post and schedule content from other models such as Articles & Reviews.

So it looks something like this:

Post

    t.datetime "scheduled_at"
    t.string "state"
    t.text "error"
    t.boolean "facebook_wall_post"
    t.boolean "facebook_page_post"
    t.boolean "twitter"
    t.boolean "linkedin"

Article

    t.string "title"
    t.text "body"
    t.string "authror"

Review:

    t.string "title"
    t.text "body"
    t.string "rating"

So a User might create a new Article and then optionally choose to post it to Facebook or they may go to /post/new and select the Article from a select field. The can also choose from multiple networks i.e. facebook, twitter etc.

So far I have something like:

class Post < ApplicationRecord
  belongs_to :user
  has_many :articles
  has_many :reviews
end
class Article < ApplicationRecord
  belongs_to :user
  belongs_to :post
end
class Review < ApplicationRecord
  belongs_to :user
  belongs_to :post
end

How would you setup this sort of relationship?