Created
June 14, 2012 23:48
-
-
Save marcamillion/2933700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Category < ActiveRecord::Base | |
has_and_belongs_to_many :users | |
attr_accessible :name | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.9.2p0 :002 > f = User.first | |
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1 | |
=> #<User id: 1, email: "[email protected]", encrypted_password: "$2a$10$LTc6zq5VorMZzsDSv7nD2eZpQkV30BaylB2Q6oY4yYjJ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2012-06-13 08:12:53", last_sign_in_at: "2012-06-13 08:09:08", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "Ayy Bee Cee", created_at: "2012-06-13 08:08:01", updated_at: "2012-06-13 08:12:53", confirmation_token: nil, confirmed_at: "2012-06-13 08:09:08", confirmation_sent_at: "2012-06-13 08:08:01", unconfirmed_email: nil, authentication_token: nil> | |
1.9.2p0 :003 > f.categories | |
Category Load (8.1ms) SELECT "categories".* FROM "categories" INNER JOIN "categories_users" ON "categories"."id" = "categories_users"."category_id" WHERE "categories_users"."user_id" = 1 | |
=> [] | |
1.9.2p0 :003 > f.categories | |
=> [] | |
1.9.2p0 :007 > f.categories.create({:id => 1}) | |
(0.1ms) begin transaction | |
SQL (0.6ms) INSERT INTO "categories" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 15 Jun 2012 00:02:37 UTC +00:00], ["name", nil], ["updated_at", Fri, 15 Jun 2012 00:02:37 UTC +00:00]] | |
(12.3ms) INSERT INTO "categories_users" ("user_id", "category_id") VALUES (1, 10) | |
(67.1ms) rollback transaction | |
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed: INSERT INTO "categories_users" ("user_id", "category_id") VALUES (1, 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.9.2p0 :008 > f.categories.create({:user_id => 1, :category_id => 1}) | |
(16.9ms) begin transaction | |
(0.1ms) rollback transaction | |
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: user_id, category_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE "categories_users" ("category_id" integer, "user_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_and_belongs_to_many :categories | |
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :confirmed_at, :confirmation_token, :categories | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment