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 8675391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:56:18+00:00 2026-06-12T19:56:18+00:00

I have my models & associations currently setup like so: class User < ActiveRecord::Base

  • 0

I have my models & associations currently setup like so:

class User < ActiveRecord::Base
  has_and_belongs_to_many :projects
  has_many :versions, :through => :projects
end

class Projects < ActiveRecord::Base
  has_many :versions
end

class Version < ActiveRecord::Base
  belongs_to :project
  attr_accessible :user_id, :project_id
  before_create :associate_user

 def associate_user
  # I have no idea what to do here - in fact, I don't think this is even the right place to do this!
 end
end

When I do something like user.projects.first.versions.create, I would like the user_id field in Version to be filled with the user_id of the user through whom the model was created. Right now, when I execute that create method, it is set to nil. Now, this makes sense, and I understand why it doesnt work. I just cant figure out how to make this work.

I’ve been scratching my head over this, and cannot figure it out! How would you accomplish this?

UPDATE

Note: although this worked, Levi’s answer below is a much, much better solution, and is what I ended up going with

I figured it out, but I would still like feedback on whether this is the best way to go about this. I feel like there may be a built-in method to rails to do this that I’m missing

Here’s my updated Version model:

class Version < ActiveRecord::Base
belongs_to :production
attr_accessible :user_id, :production_id

after_create :associate_user

def associate_user
    @users = User.all(:include => :productions, :conditions => {"productions_users.production_id" => self.production_id})
    @users.each do |user|
        user.productions.each do |production|
            if production.versions.exists?(self)
                @version_user = user
            end
        end
    end
    self.user_id = @version_user.id
 end
end
  • 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-12T19:56:19+00:00Added an answer on June 12, 2026 at 7:56 pm

    I would just pass the id in when you create the version.

    user.projects.first.version.create(:user_id => user.id)
    

    That way you don’t need the before_create callback at all.

    Edit:

    You may also want to consider your database structure. You have a project_id and a user_id on your versions table. You also have the join table (projects_users) with the same keys. Why not turn that into a real model and add a belongs_to :user_project (or whatever is appropriate) to the Version model? That is one extra join to go from Versions to Projects, but the data model makes more sense.

    class User < ActiveRecord::Base
      has_many :user_projects
      has_many :projects, :through => :user_projects
      has_many :versions, :through => :projects
    end
    
    class UserProject < ActiveRecord::Base
      belongs_to :user
      belongs_to :project
      has_many :versions
    end
    
    class Projects < ActiveRecord::Base
      has_many :versions
      has_many :user_projects
      has_many :users, :through => :user_projects
    end
    
    class Version < ActiveRecord::Base
      belongs_to :user_project
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
I have 2 models (Book & Image) class Book < ActiveRecord::Base has_many :images accepts_nested_attributes_for
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
I have two models: Program < ActiveRecord::Base has_many :events def federal_financing events.sum(&:federal_financing) end def
I have 2 models: class UserPrice < ActiveRecord::Base attr_accessible :price, :product_name belongs_to :user belongs_to
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have a visit model as following class Visit < ActiveRecord::Base belongs_to :engineer has_many
I have model: class Topic < ActiveRecord::Base attr_accessible :title, :description has_many :comments, :dependent =>
I have two rails resources: notes & technologies. Note Model: class Note < ActiveRecord::Base
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor

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.