Ruby on rails 如何正确定义此自定义标记关系?
编辑:我已经删除并重新创建了数据库,正如另一个回答中所建议的那样,没有任何更改。此外,我不能使用gem-like-act\u-as\u-taggable,因为我使用的标签与正常的不同 关于堆栈溢出的第一个问题!我很难让一个简单的关联正常工作,我不知道为什么。我花了相当多的时间查看SO和文档,在我看来,我所拥有的应该是有效的。我正在添加一个属于用户和订单的“标记”模型,并且我还向用户和订单模型添加了很多:标记。以下是我创建的模型的模式:Ruby on rails 如何正确定义此自定义标记关系?,ruby-on-rails,database,activerecord,Ruby On Rails,Database,Activerecord,编辑:我已经删除并重新创建了数据库,正如另一个回答中所建议的那样,没有任何更改。此外,我不能使用gem-like-act\u-as\u-taggable,因为我使用的标签与正常的不同 关于堆栈溢出的第一个问题!我很难让一个简单的关联正常工作,我不知道为什么。我花了相当多的时间查看SO和文档,在我看来,我所拥有的应该是有效的。我正在添加一个属于用户和订单的“标记”模型,并且我还向用户和订单模型添加了很多:标记。以下是我创建的模型的模式: create_table "tags", force: tr
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
我的订单模型的模式非常相似,唯一的区别是我没有在订单模型中添加索引
索引把我的订单弄乱了吗?是什么导致了那个严重的错误
其他信息:
我一直很难按照自己的意愿定义这个模型,所以我创建了大约三个迁移,然后回滚它们,并使用git reset--硬头回滚所有其他迁移,每次都重新开始
根据要求,我的用户模型为:
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用户
我的标签模型是
class Tag < ActiveRecord::Base
belongs_to :user
belongs_to :order
end
class标记
我找到了问题的原因,这肯定是我的错。以前版本的标记模型迁移在名为users的文件夹中留下了一个额外的tag.rb模型。我想我只是没有注意到这个文件夹,或者认为它与用户模型有关。我还以为运行git reset——硬启动它会恢复我所做的任何更改,但不知怎么的,它还是通过了——也许它没有添加到我的索引中?
在任何情况下,都是一个额外的tag.rb模型导致了问题 在我看来,您没有正确定义模型中的关联。请发布您的用户模型和标签模型的代码,谢谢回复!我已经发布了这些,但我不认为它们有任何问题。我有三种调查途径,尽管这很难从远处解决:(1)发布您的db/schema.rb以供考虑,这应该是最新的迁移,(2)确保连接到正确的数据库,以及(3)从用户中删除
rolify
和designe
,以确保它们没有意外的交互。换句话说,您的模型看起来不错。感谢您的帮助!我确实找到了一个如下所述的答案-一个额外的tag.rb model藏在我的models文件夹中的用户文件夹中。
class Tag < ActiveRecord::Base
belongs_to :user
belongs_to :order
end