rails 多个 table id 查找

rails multiple table id find

并使用rails多态关联..

这是wall_notification.rb

class WallNotification < ActiveRecord::Base
    belongs_to :attachable, polymorphic: true
    belongs_to :user
end

这是picture.rb

class Picture < ActiveRecord::Base
    belongs_to :user
    has_many :wall_notifications, as: :attachable
    mount_uploader :pic_upload
    validates :pic_upload,presence: true
end

这是user_video.rb

class UserVideo < ActiveRecord::Base
    belongs_to :user
    has_many :wall_notifications, as: :attachable
    mount_uploader :user_video
end

这是career.rb

class Career < ActiveRecord::Base
    has_many :wall_notifications, as: :attachable
    mount_uploader :career_file
end

并且,当创建新图片时,user_video,职业,wall_notifiacatin 将自动创建新记录,因为我们使用多态关联..

当我检查我的 rails 控制台时..多态关联完美工作..

[1] pry(main)> WallNotification.all
  WallNotification Load (220.3ms)  SELECT `wall_notifications`.* FROM `wall_notifications`
=> [#<WallNotification:0xb4b51d0
  id: 1,
  user_id: 1,
  attachable_id: 1,
  attachable_type: "Picture",
  created_at: Wed, 19 Oct 2016 04:50:55 UTC +00:00,
  updated_at: Wed, 19 Oct 2016 04:50:55 UTC +00:00>,
 #<WallNotification:0xb4a98e4
  id: 2,
  user_id: 1,
  attachable_id: 1,
  attachable_type: "UserVideo",
  created_at: Wed, 19 Oct 2016 04:53:43 UTC +00:00,
  updated_at: Wed, 19 Oct 2016 04:53:43 UTC +00:00>,
 #<WallNotification:0xb4a9740
  id: 3,
  user_id: 1,
  attachable_id: 1,
  attachable_type: "Career",
  created_at: Wed, 19 Oct 2016 05:12:43 UTC +00:00,
  updated_at: Wed, 19 Oct 2016 05:12:43 UTC +00:00>]

但现在我想要 current_user 的 wall_notifications 列表..这是我的 controller.rb

def index
    @wall_notifications_picture = current_user.wall_notifications.where(attachable_type: 'Picture')
    @picture_ids = @wall_notifications_picture.map {|w| Picture.find_by_id(w.attachable_id)}
    @wall_notifications_uservideo = current_user.wall_notifications.where(attachable_type: 'UserVideo')
    @uservideo_ids = @wall_notifications_uservideo.map {|w| UserVideo.find_by_id(w.attachable_id)}
    @wall_notifications_career = current_user.wall_notifications.where(attachable_type: 'Career')
    @career_ids = @wall_notifications_uservideo.map {|w| Career.find_by_id(w.attachable_id)}
 end

我希望每个 table 数据都在一个实例变量中 :( :( 谁帮帮我..

就拿一个实例hash把每条数据作为一个key来推送

def index
    @instance = {}
    @instance['wall_notifications_picture'] = current_user.wall_notifications.where(attachable_type: 'Picture')
    @instance['picture_ids'] = @instance['wall_notifications_picture'].map {|w| Picture.find_by_id(w.attachable_id)}
    @instance['wall_notifications_uservideo'] = current_user.wall_notifications.where(attachable_type: 'UserVideo')
    @instance['uservideo_ids'] = @instance['wall_notifications_uservideo'].map {|w| UserVideo.find_by_id(w.attachable_id)}
    @instance['wall_notifications_career'] = current_user.wall_notifications.where(attachable_type: 'Career')
    @instance['career_ids'] = @instance['uservideo_ids'].map {|w| Career.find_by_id(w.attachable_id)}

end

现在只发送 @instance 到视图中。

在视图中,

<%= @instance %>

您可以使用其中的每个键在相应的位置显示。

谢谢大家..现在我找到了简单的步骤

在我的 controller.rb

def index
    @wall_notifications = current_user.wall_notifications.order("id DESC")
end

和我的view.html.erb

<% if current_user.wall_notifications.present? %>
  <!-- wall notification picture-->
  <% @wall_notifications.each do |wall_notification|%>
  <div class="row margin-top">
    <div class="col-md-12">
      <div class="right-article">    
        <div class="item-side-article">
          <% if wall_notification.attachable_type == 'Picture'%>
            <% if wall_notification.attachable.pic_upload.file.extension.downcase == 'mp4'%>
               <video style="float:left;height:102px;margin-right:10px;width:180px;" controls>
                  <source src="<%= wall_notification.attachable.pic_upload.url%>" type="video/mp4">
                Your browser does not support the video tag.
                </video>
            <% else %>
              <img src="<%= wall_notification.attachable.pic_upload.url%>">
            <% end %>
          <% end %>
          <% if wall_notification.attachable_type == 'UserVideo'%>
            <% if wall_notification.attachable.user_video.present? %>
              <video style="float:left;height:102px;margin-right:10px;width:180px;" controls>
                <source src="<%= wall_notification.attachable.user_video.url%>" type="video/mp4">
                Your browser does not support the video tag.
              </video>
            <% end %>
          <% end %>
          <% if wall_notification.attachable_type == 'Career'%>
            <% if wall_notification.attachable.career_file.present?%>
              <img src="<%= wall_notification.attachable.career_file.url%>">
            <% end %>
          <% end %>
        </div>
      </div>
    </div>
  </div>
  <%end%>
  <!-- wall notification picture end -->
<% end %>