[英]How do I define this custom tag relationship properly?
編輯:我已刪除並重新創建數據庫,如在其他答案中所建議,沒有任何更改。 另外,我不能使用act_as_taggable這樣的gem,因為我使用的標簽與正常情況不同。
堆棧溢出的第一個問題! 我很難獲得一個非常簡單的關聯才能正常工作,但我不確定為什么。 我花了很多時間看SO和文檔,在我看來,我應該做的工作。 我正在添加一個屬於用戶和訂單的“標簽”模型,並且還向用戶和訂單模型添加了has_many:tags。 這是我創建的模型的架構:
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
在rails控制台中,我可以創建一個帶有用戶關聯的標簽:
this_tag = Tag.new(name:"Urgent", color: "red", user_id: 1)
我可以讓我的標簽用戶
this_tag.user
但是我無法使用
a_user = User.first
a_user.tags
這給了我:
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
這似乎是一個奇怪的錯誤,並且所有提到它的SO帖子都似乎沒有像我這樣的問題。 我可以通過以下方式獲得用戶訂單
a_user.orders
我的訂單模型的架構非常相似,唯一的區別是我沒有在訂單模型中添加索引...
索引搞亂了我的訂單嗎? 是什么導致該令人討厭的錯誤?
附加信息:
我一直在定義我想要的模型時遇到了麻煩,因此我創建了大約三個遷移,然后回滾了它們,並使用git reset --hard HEAD回滾了所有其他內容,並每次都重新開始。
根據要求,我的用戶模型是:
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
而我的標記模型是
class Tag < ActiveRecord::Base
belongs_to :user
belongs_to :order
end
我找到了問題的原因,這絕對是我的錯。 標記模型遷移的先前版本在名為users的文件夾中保留了一個額外的tag.rb模型。 我想我只是沒有注意到該文件夾,或者認為它與用戶模型有關。 我還以為在它上面運行git reset --hard HEAD可以恢復我所做的任何更改,但是以某種方式漏掉了它-也許它沒有添加到我的索引中? 無論如何,都是導致問題的額外的tag.rb模型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.