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 ..
können Sie einige Pseudo-Code schreiben darüber, wie finden Sie möchten Ihre Daten? – Sajan