ruby on rails - I can't fetch any rows from a Polymorphic association -
i've set 2 polymorphic associations on table, have no problem adding table can't seem retrieve added information.
here's i'm doing:
post
belongs_to :posted, polymorphic: true belongs_to :received, polymorphic: true
user
has_many :posted_posts, class_name: 'post', as: :posted has_many :received_posts, class_name: 'post', as: :received
group
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 |format| format.html {redirect_to root_url} format.js end else respond_to |format| format.html {redirect_to root_url} format.js end end end
post seems fine, should able retrieve information using
<% @user.received_posts |post| %> <%= post.content %> <% end %> <% @user.posted_posts |post| %> <%= post.content %> <% end %>
but neither of these work. missing? it's not bringing error. in fact, logs aren't showing anything.. it's it's being ignored. need method somewhere lifting? though rest handled rails..
any appreciated.
update - logs
started "/!/david" 127.0.0.1 @ 2014-08-18 12:02:32 +0100 processing userscontroller#show html parameters: {"username"=>"david"} user load (0.3ms) select "users".* "users" "users"."id" = 2 order "users"."id" asc limit 1 user load (0.2ms) select "users".* "users" (username == 'david') limit 1 (0.2ms) select count(*) "follows" "follows"."followable_id" = ? , "follows"."followable_type" = ? , "follows"."blocked" = 'f' [["followable_id", 3], ["followable_type", "user"]] (0.2ms) select count(*) "follows" "follows"."blocked" = 'f' , "follows"."follower_id" = 2 , "follows"."follower_type" = 'user' , "follows"."followable_id" = 3 , "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".* "mailboxer_conversations" inner join "mailboxer_notifications" on "mailboxer_notifications"."conversation_id" = "mailboxer_conversations"."id" , "mailboxer_notifications"."type" in ('mailboxer::message') inner join "mailboxer_receipts" on "mailboxer_receipts"."notification_id" = "mailboxer_notifications"."id" "mailboxer_notifications"."type" = 'mailboxer::message' , "mailboxer_receipts"."receiver_id" = 2 , "mailboxer_receipts"."receiver_type" = 'user' order mailboxer_conversations.updated_at desc mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 37]] user load (0.1ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 44]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 45]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 46]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 43]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 42]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 41]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 40]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 39]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 38]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 36]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.4ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 35]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 34]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 33]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.4ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 32]] cache (0.1ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 31]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 30]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 29]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 28]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 27]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 26]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 25]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 24]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 23]] cache (0.1ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 22]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 21]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 20]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 19]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 18]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 17]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 16]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 15]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 14]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 13]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 12]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 11]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 10]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 9]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 8]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 7]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 6]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 5]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.3ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 4]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 3]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 2]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 2]] mailboxer::message load (0.2ms) select "mailboxer_notifications".* "mailboxer_notifications" "mailboxer_notifications"."type" in ('mailboxer::message') , "mailboxer_notifications"."conversation_id" = ? order created_at desc limit 1 [["conversation_id", 1]] cache (0.0ms) select "users".* "users" "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)
turned out missed .each
:
<% @user.posted_posts.each |post| %>
Comments
Post a Comment