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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:36:35+00:00 2026-06-08T11:36:35+00:00

Ok so i have this data structure class User < ActiveRecord::Base has_many :companies, :through

  • 0

Ok so i have this data structure

class User < ActiveRecord::Base
  has_many :companies, :through => :positions
  has_many :positions

class Company < ActiveRecord::Base
  has_many :positions
  has_many :users, :through => :positions

class Position < ActiveRecord::Base
  belongs_to :company
  belongs_to :user
  attr_accessible :company_id, :user_id, :regular_user
end

And my database structure

create_table "positions", :force => true do |t|
  t.integer  "company_id"
  t.integer  "user_id"
  t.datetime "created_at",                     :null => false
  t.datetime "updated_at",                     :null => false
  t.boolean  "regular_user", :default => true
end

And if i add another company to a users companies, the regular_user flag is always set to true

1.9.3-p125 :013 > @user.companies << Company.last
  Company Load (0.3ms)  SELECT `companies`.* FROM `companies` 
  ORDER BY `companies`.`id` DESC LIMIT 1
   (0.0ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `positions` 
(`company_id`, `created_at`, `regular_user`, `updated_at`, `user_id`) 
VALUES 
(263, '2012-07-25 13:56:56', 1, '2012-07-25 13:56:56', 757)

Is there a way to set the flag to false before the insert

I have been getting around it by doing this….which is hackish

@user.positions.update_all(:regular_user => false) if @user.is_admin?

Is there another way (cleaner) to achieve this

  • 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-08T11:36:36+00:00Added an answer on June 8, 2026 at 11:36 am

    Use a before_save filter.
    Ex.:

    class Position
    
      before_save :set_regular_user_to_false
    
      def set_regular_user_to_false
        self.regular_user = false
      end
    
    end
    

    Like the filter name says, this will intercept the chain of events right before saving the position object, so you can change the regular_user attribute.

    EDIT

    def set_regular_user_to_false
      if self.user.user_type != 'admin'
        self.regular_user = false
      end
      true
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for
I have this HTML structure <div class=buttons> <button data-icon=ui-icon-disk class=save>Save1</button> <button data-icon=ui-icon-check class=ok>OK1</button> <button
I'm using PostgreSQL 9.1 and I have this data structure: A B ------- 1
i have data structure i am passing this from server to client using data
My table have data structure like this cate_id task_id date_start date_end other 34 14
Using EF4 and Linq I have a data structure which looks like this: parentid
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have some data and need to create a json file with this structure
This is the common structure of all of my classes: public class User {
I'm a bit confused on this. I have a data table structured like this:

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.