karthick nagarajan karthick nagarajan - 1 month ago 5
Ruby Question

rails multiple table id find

and use rails polymorphic association..

This is wall_notification.rb

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


This is 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


This is user_video.rb

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


This is career.rb

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


and,when create new picture,user_video,career, wall_notifiacatin will create new record automatically because,we use polymorphic association..

when i check my rails console..polymorphic association perfectly working..

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


but now i want to current_user's wall_notifications list..and this is my 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 want each table data in one instance variable :( :(
Any one help me..

Answer

Just take an instance hash and push each data as a 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

Now send only @instance into the view.

in the view,

<%= @instance %>

You can use each key in it to display in respective places.