我在一个表上设置了2个多态关联,我没有问题添加到表中但我似乎无法检索添加的信息。
这就是我正在做的事情:
发布
belongs_to :posted, polymorphic: true
belongs_to :received, polymorphic: true
用户
has_many :posted_posts, class_name: 'Post', as: :posted
has_many :received_posts, class_name: 'Post', as: :received
组
has_many :posted_posts, class_name: 'Post', as: :posted
has_many :received_posts, class_name: 'Post', as: :received
users_controller.rb
def post
authorize @user
@post = Post.new(post_params)
@post.received = @user
@post.posted = @current_user
if @post.save
respond_to do |format|
format.html {redirect_to root_url}
format.js
end
else
respond_to do |format|
format.html {redirect_to root_url}
format.js
end
end
end
帖子似乎很好,但我应该能够使用
检索信息 <% @user.received_posts do |post| %>
<%= post.content %>
<% end %>
<% @user.posted_posts do |post| %>
<%= post.content %>
<% end %>
但这些都不奏效。我错过了什么?它没有引起错误。事实上,日志没有显示任何东西..它就像被忽略了。我是否需要在某处提升某种方法?我虽然其余的是由铁路处理..
任何帮助都将不胜感激。
更新 - 记录
Started GET "/!/David" for 127.0.0.1 at 2014-08-18 12:02:32 +0100
Processing by UsersController#show as HTML
Parameters: {"username"=>"David"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE (username == 'David') LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."followable_id" = ? AND "follows"."followable_type" = ? AND "follows"."blocked" = 'f' [["followable_id", 3], ["followable_type", "User"]]
(0.2ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."blocked" = 'f' AND "follows"."follower_id" = 2 AND "follows"."follower_type" = 'User' AND "follows"."followable_id" = 3 AND "follows"."followable_type" = 'User'
Rendered users/_links.html.erb (1.2ms)
Rendered users/show.html.erb within layouts/application (7.8ms)
Rendered layouts/_app_sidebar.html.erb (0.8ms)
Mailboxer::Conversation Load (0.6ms) SELECT DISTINCT "mailboxer_conversations".* FROM "mailboxer_conversations" INNER JOIN "mailboxer_notifications" ON "mailboxer_notifications"."conversation_id" = "mailboxer_conversations"."id" AND "mailboxer_notifications"."type" IN ('Mailboxer::Message') INNER JOIN "mailboxer_receipts" ON "mailboxer_receipts"."notification_id" = "mailboxer_notifications"."id" WHERE "mailboxer_notifications"."type" = 'Mailboxer::Message' AND "mailboxer_receipts"."receiver_id" = 2 AND "mailboxer_receipts"."receiver_type" = 'User' ORDER BY mailboxer_conversations.updated_at DESC
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 37]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 44]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 45]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 46]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 43]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 42]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 41]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 40]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 39]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 38]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 36]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.4ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 35]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 34]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 33]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.4ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 32]]
CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 31]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 30]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 29]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 28]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 27]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 26]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 25]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 24]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 23]]
CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 22]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 21]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 20]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 19]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 18]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 17]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 16]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 15]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 14]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 13]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 12]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 11]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 10]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 9]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 8]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 7]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 5]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 4]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 3]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 2]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Rendered mailboxer/conversations/_conversation.html.erb (155.4ms)
Rendered layouts/_app_header.html.erb (159.5ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 223ms (Views: 206.2ms | ActiveRecord: 13.5ms)
答案 0 :(得分:1)
原来你错过了.each
:
<% @user.posted_posts.each do |post| %>