简体   繁体   中英

How do I define this custom tag relationship properly?

EDIT: I've dropped and recreated the database, as suggested in a different answer, with no change. Also, I can't use a gem like acts_as_taggable, because I'm using my tags differently than normal.

First question on stack overflow! I'm having a bit of trouble getting a very simple association to work properly, and I'm not sure why. I've spent quite a bit of time looking at SO and docs, and it seems to me that what I have should be working. I am adding a "tag" model that belongs_to users and orders, and I've also added has_many :tags to the user and order models. Here is the schema of the model I've created:

create_table "tags", force: true do |t|
    t.string   "name"
    t.string   "color"
    t.integer  "order_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
end

add_index "tags", ["order_id"], name: "index_tags_on_order_id", using: :btree
add_index "tags", ["user_id"], name: "index_tags_on_user_id", using: :btree

In the rails console, I can create a tag, with a user association:

this_tag = Tag.new(name:"Urgent", color: "red", user_id: 1)

and I can get my tags user with

this_tag.user

but I can't get my users tags with

a_user = User.first
a_user.tags

which gives me:

User Load (0.9ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
 => #<User id: 1, email: "redacted", encrypted_password: "$2a$10$kdEgXW61I3b3Bu9Rbs3W0ex1jxTmIFWVe1jabDY9q9.U...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 5, current_sign_in_at: "2013-10-29 22:12:57", last_sign_in_at: "2013-10-29 21:59:13", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-10-23 23:49:00", updated_at: "2013-10-29 22:12:57", name: "Jordan"> 
2.0.0-p247 :006 > me.tags
PG::UndefinedTable: ERROR:  relation "user_tags" does not exist
LINE 5:                WHERE a.attrelid = '"user_tags"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"user_tags"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "user_tags" does not exist
LINE 5:                WHERE a.attrelid = '"user_tags"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"user_tags"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

which is seems like an odd error, and none of the SO posts that refer to it seem to be having a problem that is like mine. I can get my users orders with

a_user.orders

and my order model's schema is very similar, the only difference being that I don't have an index added to my order model...

Is the index messing up my order? What is causing that nasty error?

ADDITIONAL INFO:

I've been having trouble defining this model how I want it, so I have created about three migrations, and then rolled them back, and used git reset --hard HEAD to roll everything else back and start again each time.

As requested, my user model is:

class User < ActiveRecord::Base
    has_many :orders
    has_many :tags
  rolify

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

and my tag model is

class Tag < ActiveRecord::Base
  belongs_to :user
  belongs_to :order
end

I found the cause of the problem, and it was definitely my fault. A previous version of the tag model migration had left an extra tag.rb model inside a folder called users. I guess I just didn't notice the folder, or thought that it was related to the User model. I also would have thought that running git reset --hard HEAD on it would have reverted any changes that I made, but this slipped through somehow - perhaps it wasn't added to my index? In any case, it was an extra tag.rb model that was causing the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM