Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 806137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:11:49+00:00 2026-05-15T00:11:49+00:00

When building a rails app that allows a User to login and create data,

  • 0

When building a rails app that allows a User to login and create data, is it best to setup a belongs_to :user association on every single model? For example, let’s say a user can create Favorites, Colors and Tags.

And let’s say Favorites has_many :tags and Colors also has_many :tags. Is it still important for Tags to belong_to :user assuming the User is the only person who has authority to edit those tags?

And a similar question along the same lines: When updating data in FavoritesController, I’ve come to the conclusion that you perform CRUD operations by always doing something like current_user.favorites.find(param[:id].update_attributes(param[:favorite]) so that they can definitely only update models that belong to them. Right?

Update Wasn’t too happy with any of the answers, as no one really answered my question but instead went after the for-example-only Tags model suggesting better ways to do that. I’m assuming I was right, and models should belong_to :user. I also discovered some great security tips that address my questions here: http://asciicasts.com/episodes/178-seven-security-tips

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:11:50+00:00Added an answer on May 15, 2026 at 12:11 am

    As you describe the tags it seems that they are more of an aspect, so you can implement them as a polymorphic association. But you should do it many-to-many, as tags can be reused among users and taggable objects. Let’s call the join model Tagging, which will be the one that belongs to user if you want to remember who created the tagging.

    class Tag < ActiveRecord::Base
      has_many :taggings, :dependent => :destroy
      has_many :colors, :through => :taggings, :source => :taggable, :source_type => "Color"
      has_many :favorites, :through => :taggings, :source => :taggable, :source_type => "Favorite"
    end
    
    class Tagging < ActiveRecord::Base
      belongs_to :user
      belongs_to :taggable, :polymorphic => true
      belongs_to :tag  
    end
    
    class Color < ActiveRecord::Base
      belongs_to :user
      has_many :taggings, :as => :taggable
      has_many :tags, :through => :taggings
    end
    
    class Favorite < ActiveRecord::Base
      belongs_to :user
      has_many :taggings, :as => :taggable
      has_many :tags, :through => :taggings
    end
    
    class User < ActiveRecord::Base
      has_many :favorites
      has_many :colors
      has_many :taggings
      has_many :tags, :through => :taggings
    end
    

    As for the Favorite updating, I agree with you: you will mostly work within the scope of a user (most likely the currently logged in user).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Those are linker errors. The Boost regex library is not… May 15, 2026 at 6:48 am
  • Editorial Team
    Editorial Team added an answer I turned of VERBOSE linking and set it to none.… May 15, 2026 at 6:48 am
  • Editorial Team
    Editorial Team added an answer static Random random = new Random(); static int CheatToWin() {… May 15, 2026 at 6:48 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.