Skip to content

Instantly share code, notes, and snippets.

@IvanShamatov
Created November 15, 2015 17:36
Show Gist options
  • Save IvanShamatov/917e6962dbb1e1f8b2ab to your computer and use it in GitHub Desktop.
Save IvanShamatov/917e6962dbb1e1f8b2ab to your computer and use it in GitHub Desktop.
class Good < ActiveRecord::Base
has_many :good_tags
has_many :tags, through: :good_tags
end
class GoodTag < ActiveRecord::Base
belongs_to :good
belongs_to :tag
end
class Tag < ActiveRecord::Base
has_many :good_tags
has_many :goods, through: :good_tags
end
2.1.1 :002 > g = Good.create
(0.2ms) begin transaction
SQL (0.7ms) INSERT INTO "goods" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-11-15 17:35:13.480173"], ["updated_at", "2015-11-15 17:35:13.480173"]]
(0.6ms) commit transaction
=> #<Good id: 1, created_at: "2015-11-15 17:35:13", updated_at: "2015-11-15 17:35:13">
2.1.1 :003 > g.tags.create
(0.0ms) begin transaction
SQL (0.5ms) INSERT INTO "tags" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-11-15 17:35:23.303590"], ["updated_at", "2015-11-15 17:35:23.303590"]]
SQL (0.2ms) INSERT INTO "good_tags" ("good_id", "tag_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["good_id", 1], ["tag_id", 1], ["created_at", "2015-11-15 17:35:23.321685"], ["updated_at", "2015-11-15 17:35:23.321685"]]
(0.8ms) commit transaction
=> #<Tag id: 1, created_at: "2015-11-15 17:35:23", updated_at: "2015-11-15 17:35:23">
2.1.1 :004 > g.tags.create
(0.1ms) begin transaction
SQL (1.8ms) INSERT INTO "tags" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-11-15 17:35:34.638708"], ["updated_at", "2015-11-15 17:35:34.638708"]]
SQL (0.2ms) INSERT INTO "good_tags" ("good_id", "tag_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["good_id", 1], ["tag_id", 2], ["created_at", "2015-11-15 17:35:34.643055"], ["updated_at", "2015-11-15 17:35:34.643055"]]
(1.7ms) commit transaction
=> #<Tag id: 2, created_at: "2015-11-15 17:35:34", updated_at: "2015-11-15 17:35:34">
2.1.1 :005 > g.tags.create
(0.1ms) begin transaction
SQL (1.3ms) INSERT INTO "tags" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-11-15 17:35:35.425569"], ["updated_at", "2015-11-15 17:35:35.425569"]]
SQL (0.2ms) INSERT INTO "good_tags" ("good_id", "tag_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["good_id", 1], ["tag_id", 3], ["created_at", "2015-11-15 17:35:35.429225"], ["updated_at", "2015-11-15 17:35:35.429225"]]
(1.6ms) commit transaction
=> #<Tag id: 3, created_at: "2015-11-15 17:35:35", updated_at: "2015-11-15 17:35:35">
2.1.1 :006 > Good.last.tags
Good Load (0.2ms) SELECT "goods".* FROM "goods" ORDER BY "goods"."id" DESC LIMIT 1
Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "good_tags" ON "tags"."id" = "good_tags"."tag_id" WHERE "good_tags"."good_id" = ? [["good_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Tag id: 1, created_at: "2015-11-15 17:35:23", updated_at: "2015-11-15 17:35:23">, #<Tag id: 2, created_at: "2015-11-15 17:35:34", updated_at: "2015-11-15 17:35:34">, #<Tag id: 3, created_at: "2015-11-15 17:35:35", updated_at: "2015-11-15 17:35:35">]>
create_table "good_tags", force: :cascade do |t|
t.integer "good_id"
t.integer "tag_id"
t.string "othervalue"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goods", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "tags", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment