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

  • Home
  • SEARCH
  • 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 8290005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:40:09+00:00 2026-06-08T12:40:09+00:00

I have set up a many-to-many association between users and rotas. I am trying

  • 0

I have set up a many-to-many association between users and rotas. I am trying to also store who the creator of a particular rota is.

Class User

  has_many :rotazations
  has_many :rotas, :through => :rotazations

Class Rota

  has_many :rotazations
  has_many :users, :through => :rotazations, :order => 'name ASC'

Class Rotazation

  belongs_to :user
  belongs_to :rota

  before_create do |rotazation|
    rotazation.creator_id = rotazation.user_id
  end

When a rota is created, I have successfully set the creator_id in the rotazation table to have the id of the user who created the rota. My question is, how do I access it:

rota.creator

My attempt

I have tried to add the following:

Rota Class

...
belongs_to :creator, :class_name => "User", :through => :rotazations, :foreign_key => "creator_id"  
...

User Class

has_many :created_rotas, :class_name=> "Rota", :through => :rotazations, :foreign_key => "creator_id"  

However, this doesnt work. Any ideas how to achieve rota.creator work? Thanks!

  • 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-06-08T12:40:11+00:00Added an answer on June 8, 2026 at 12:40 pm

    Each time you create a new line in your rotazations table you add the user_id to the creator_id, which is an attribute on the Rotazation.

    However, the rotazations table is a combination of user_id and rota_id just to solve the many-to-many relationship. If I am thinking it right, the creator_id will always be the same as user_id. So, you actually have redundante data:

    | user_id | rota_id |creator_id |
    | 1       | 1       | 1         |
    | 2       | 1       | 2         |
    | 1       | 2       | 1         |
    | ...     | ...     | ...       |
    

    In order to know who has created a new Rota you can add a new attribute to the rotas table, lets say creator.

    When you create a new rota, you can save the user_id into this new attribute:

    @rota = Rota.new
    @rota.creator = current_user.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set up a Many-to-Many association between 2 objects. I have
Hello I've set up a One-to-Many Association between orders and users. I'm wondering how
my issue lokks similar to this one: (link) but i have one-to-many association: <set
In my web application we have many users.I want to set permission for each
I have a Users table and a Networks table with a many-to-many relationship between
I have two dependent models with one to many association class Post < ActiveRecord::Base
I have a standard many-to-many relationship between users and roles in my Rails app:
I have a has_many and belongs_to association set up between two models: Project and
I have a Customer and Phone class with one to many association with lazy
I am using HIbernate 3.2.5. I have a one-to-many association between Dept and Training

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.