Has_many班級名稱不像我想的那樣工作?

[英]Has_many class name not working like I thought?


I have a user and post model:

我有一個用戶和帖子模型:

class User < ActiveRecord::Base
  has_many :sent_posts, :class_name => 'Post'
end

class Post < ActiveRecord::Base
  belongs_to :user
end

The problem is that in the console, if I do

問題是在控制台中,如果我這樣做

User.first.sent_posts.empty?

it returns True.

它返回True。

But if I do this in my view

但如果我在我看來這樣做

<%= @user.sent_posts.empty? %>

it returns False. Any ideas why this is happening? It works fine if I just use

它返回False。任何想法為什么會這樣?如果我只是使用它,它工作正常

has_many :posts

on its own.

在其自己的。

Thanks

2 个解决方案

#1


Are you sure that User.first and @user are the same User model record?

您確定User.first和@user是相同的用戶模型記錄嗎?

#2


Figured it out. It was to do with having an @user.sent_posts.build in the controller! Doh.

弄清楚了。這與在控制器中擁有@ user.sent_posts.build有關!衛生署。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/04/72a28662968fe841c5904bed03fc417.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com