Need data from rails join table, has_many :through

前端 未结 3 921
眼角桃花
眼角桃花 2020-12-05 11:57

I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id. This would

相关标签:
3条回答
  • 2020-12-05 12:15

    For people using rails 4, the usage of :order, :select, etc has been deprecated. Now you need to specify as follows:

    has_many :users, -> { select 'users.*, follows.is_admin as is_follow_admin' }, :through => :follows
    
    0 讨论(0)
  • 2020-12-05 12:18

    To do this you need to use a bit of SQL in the has_many. Something like this should hopefully work. has_many :users, :through => :follows, :select => 'users.*, follows.is_admin as is_follow_admin'

    Then in the loop you should have access to user.is_follow_admin

    0 讨论(0)
  • 2020-12-05 12:32

    You might be able to do something like:

    <% things.follows.each do |follow| %>
      <%= follow.relation %>
      <%= follow.user %>
    <% end %>
    
    0 讨论(0)
提交回复
热议问题