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

The Archive Base Latest Questions

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

Here is my data structure class User < ActiveRecord::Base has_many :companies, :through => :positions

  • 0

Here is my 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

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

  def set_regular_user
    if self.user.is_admin?
      self.regular_user = false
    else
      self.regular_user = true
    end
  end
end

everytime i run

@user.companies << Company.last

I get ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved

but if i remove my before filter everthing works out perfect and it saves as expected

 @user.companies << Company.last
     Company Load (0.2ms)  SELECT `companies`.* FROM `companies` ORDER BY `companies`.`id` DESC LIMIT 1
      (0.1ms)  BEGIN
     SQL (0.2ms)  INSERT INTO `positions` (`company_id`, `created_at`, `regular_user`, `updated_at`, `user_id`)
      VALUES 
      (263, '2012-07-25 14:44:15', NULL, '2012-07-25 14:44:15', 757)

Any ideas what i am missing ….this question is based on this earlier question

  • 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:17:53+00:00Added an answer on June 8, 2026 at 12:17 pm

    Callbacks need to return true in order to continue, false cancels the operation. In your function, the value of the if statement may be false: self.regular_user = false The return value of a ruby function is the last statement.

    Just add a return true to the end.

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

Sidebar

Related Questions

Here is the basic structure of my User class: class User { private $_userId
Here is my data structure alt text http://luvboy.co.cc/images/db.JPG when i try this sql select
Is there a .NET data structure I could use for bidirectional lookup? Here's the
I have a problem here that requires to design a data structure that takes
Here is a table structure (e.g. test): Field Name Data Type id BIGINT (20)
I need to protect the access to a data structure in my class. As
A novice R user here. So i have a data set formated like: Date
i am having a html structure like <ul> <li> <span class=vcard> <a class=underline user-link
Here is a tricky data structure and data organization case. I have an application
I'm writing a class in Java to represent a Graph data structure. This is

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.