2016-10-19 1 views
0

und Verwendung Schienen polymorphe Assoziation finden ..Schienen mehrere Tabellen ID

Diese wall_notification.rb ist

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

Dies ist 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 

Dies ist user_video.rb

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

Dies ist career.rb

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

und, wenn neues Bild erstellen, user_video, Karriere, wird wall_notifiacatin neuen Datensatz erstellen automatisch, weil wir polymorphe Assoziation verwenden ..

, wenn ich meine Schienen überprüfen console..polymorphic Verein perfekt arbeiten ..

[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>] 

aber jetzt will ich current_user des wall_notifications list..and das ist mein 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 

i jeweils Tabellendaten in einer Instanzvariable will :(:( einer mir helfen ..

+0

können Sie einige Pseudo-Code schreiben darüber, wie finden Sie möchten Ihre Daten? – Sajan

Antwort

0

Nur eine Instanz nehmen hash und jeweils Daten als Schlüssel drücken.

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 

Jetzt senden Sie nur @instance in die Ansicht.

in der Ansicht,

<%= @instance %>

können Sie jeden Schlüssel in der es verwenden in entsprechenden Stellen angezeigt werden soll.

+0

@karthick nagarajan, das versucht? – Sravan

+0

hai @sravan ich finde eine andere Antwort –

+0

Mine funktioniert? Wenn es funktioniert hat, habe ich es zumindest verbessert, damit ich denken kann, dass das nicht falsch ist. :) – Sravan

0

Dank all..now i einfachen Schritt

in meinem controller.rb

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

und meine 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 %> 
+1

@sravan nur überprüfen, dass ... ich will so .... –