rails多表id找到

时间:2016-10-19 07:20:30

标签: ruby-on-rails ruby ruby-on-rails-3

并使用rails polymorphic association ..

这是wall_notification.rb

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

这是picture.rb

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

这是user_video.rb

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

这是career.rb

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

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

当我检查我的rails console..polymorphic association完全正常工作..

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

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

/

我希望每个表数据在一个实例变量中:( :( 任何人帮助我..

2 个答案:

答案 0 :(得分:0)

只需获取一个实例hash并将每个数据作为键推送。

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 %>

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

答案 1 :(得分:0)

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

在我的controller.rb

params.permit(:first_name, :last_name, :email, :contact_number, :user_name, :password, roles: [])

和我的view.html.erb

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