简体   繁体   English

如何正确定义此自定义标签关系?

[英]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. 另外,我不能使用act_as_taggable这样的gem,因为我使用的标签与正常情况不同。

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. 我花了很多时间看SO和文档,在我看来,我应该做的工作。 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. 我正在添加一个属于用户和订单的“标签”模型,并且还向用户和订单模型添加了has_many:tags。 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: 在rails控制台中,我可以创建一个带有用户关联的标签:

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. 这似乎是一个奇怪的错误,并且所有提到它的SO帖子都似乎没有像我这样的问题。 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. 我一直在定义我想要的模型时遇到了麻烦,因此我创建了大约三个迁移,然后回滚了它们,并使用git reset --hard HEAD回滚了所有其他内容,并每次都重新开始。

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. 标记模型迁移的先前版本在名为users的文件夹中保留了一个额外的tag.rb模型。 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? 我还以为在它上面运行git reset --hard HEAD可以恢复我所做的任何更改,但是以某种方式漏掉了它-也许它没有添加到我的索引中? In any case, it was an extra tag.rb model that was causing the issue. 无论如何,都是导致问题的额外的tag.rb模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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