【发布时间】:2014-03-24 16:19:29
【问题描述】:
我阅读了很多关于关联的资料(belongs_to、has_many ...),但我想我在某个地方漏掉了一点,我找不到正确的答案。
这是我的帖子架构:
create_table "posts", force: true do |t|
t.string "titre"
t.text "description"
t.string "hastag"
t.string "postimg"
t.integer "utilisateur_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "posts", ["utilisateur_id"], name: "index_posts_on_utilisateur_id", using: :btree
当我创建一个新帖子时,我以这种方式发送“utilisateur_id”:
@post = Post.new(post_params)
current_user = session[:utilisateur_id]
@post.utilisateur_id = current_user
在 Rails 控制台中,当我输入
user = Utilisateur.find(id)
user.posts
rails 控制台找到与用户关联的帖子,是否意味着关联有效?
在我的帖子模型中,我写了belongs_to :utilisateur, :dependent => :destroy,在我的实用模型中,我写了has_many posts。
当我销毁用户时,与之关联的帖子并没有被销毁,我不明白为什么。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby associations