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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:32:25+00:00 2026-06-19T02:32:25+00:00

We’re using CanCan and managing roles in our database, using the instructions at https://github.com/ryanb/cancan/wiki/Separate-Role-Model

  • 0

We’re using CanCan and managing roles in our database, using the instructions at https://github.com/ryanb/cancan/wiki/Separate-Role-Model apart from that we’re using has_and_belongs_to_many rather than “has_many :through”

We have the following in our schema:

create_table "roles", :force => true do |t|
  t.string   "name",        :null => false
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
end

create_table "members_roles", :force => true do |t|
  t.integer  "member_id"
  t.integer  "role_id"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
end

In our models, we have:

class Member < ActiveRecord::Base
  has_and_belongs_to_many :roles
end


class Role < ActiveRecord::Base
  has_and_belongs_to_many :members
end

Now, when I try and give a member a new role, it dies. For example, in the console:

1.9.3p194 :001 > m = Member.find('test1')
  Member Load (0.3ms)  SELECT "members".* FROM "members" WHERE "members"."slug" = 'test1' LIMIT 1
 => #<Member id: 1, email: "test1@example.com", encrypted_password: "$2a$10$4VWJX07qwB9Lu5vtGAoQgOcCCRD9i8cgcxN8KGHBMaNL...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: "2013-02-14 03:58:24", confirmation_sent_at: "2013-02-14 03:58:23", unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2013-02-14 03:58:23", updated_at: "2013-02-14 03:58:24", login_name: "test1", slug: "test1", tos_agreement: nil, show_email: nil, location: nil, latitude: nil, longitude: nil> 
1.9.3p194 :002 > Role.all
  Role Load (0.1ms)  SELECT "roles".* FROM "roles" 
 => [] 
1.9.3p194 :003 > r = Role.create(:name => 'foo')
   (0.1ms)  begin transaction
  SQL (6.5ms)  INSERT INTO "roles" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", Thu, 14 Feb 2013 04:01:29 UTC +00:00], ["description", nil], ["name", "foo"], ["updated_at", Thu, 14 Feb 2013 04:01:29 UTC +00:00]]
   (746.2ms)  commit transaction
 => #<Role id: 1, name: "foo", description: nil, created_at: "2013-02-14 04:01:29", updated_at: "2013-02-14 04:01:29"> 
1.9.3p194 :004 > m.roles << r
   (0.1ms)  begin transaction 
   (0.3ms)  INSERT INTO "members_roles" ("member_id", "role_id") VALUES (1, 1)
   (0.1ms)  rollback transaction
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed: INSERT INTO "members_roles" ("member_id", "role_id") VALUES (1, 1)

(then a whole lot of traceback which I won’t bother to reproduce unless someone asks for it.)

Why on earth are we getting this ConstraintException?

  • 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-19T02:32:26+00:00Added an answer on June 19, 2026 at 2:32 am

    I believe you need to create the members_roles table without a primary key to use the has_and_belongs_to_many association by specifying :id => false in the create_table method in your migration.

    There’s an example of how to do this in the rails docs: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.