如何访问foreign_key?

时间:2014-07-31 11:17:32

标签: ruby-on-rails ruby activerecord ruby-on-rails-4 rails-activerecord

  • 用户模型/表格,
  • 日程表 - 模型/表格和
  • a users_schedules-table。

这些模型彼此之间有___belongs_to_many 我可以在控制器中添加@user.schedules << @schedule的关系
如何访问加入表&#39; users_schedules&#39; ?

我想向用户显示has_and_belongs_to_many日程安排:
我想到了这样的事情:schedules.users_belongs_to。正如您在下面的视图代码中看到的那样 查看(我想添加一个例子)

<table class="table table-hover">
        <tbody>
          <% @user_schedules_date.sort.each do |date_time, schedules| %>
            <tr class="thead success">
              <th colspan="4" scope="col"><p><%= date_time.strftime("%A, %d.%m.%Y") %></p></th>
            </tr>
            <% for schedule in schedules %>
              <tr>
                <th scope="row"><p><%= schedule.titel %></p></th>
 <td><p><%= schedules.users_belongs_to #ALL USERS WHO ARE BINDED TO THIS SCHEDULE# %></p></td>
                <td><p><%= schedule.date_time.strftime("%H:%M:%S") %></p></td>
                <td><p><%= schedule.location %></p></td>
                <td>
                  <p>
                    <%= link_to 'Bearbeiten', {:controller => 'schedule', :action => 'edit', :id => schedule.id} %>
                    oder
                    <%= link_to 'löschen', {:controller => 'schedule', :action => 'delete', :id => schedule.id} %>
                  </p>
                </td>
              </tr>
            <% end %>
          <% end %>
        </tbody>
      </table>


在控制器中,我尝试了以下操作,但我不知道如何更换/适合占位符(:email)
控制器:

class WelcomeController < ApplicationController

  def index
    if(current_user)
      @user_schedules = current_user.schedules
      @user_schedules_date = @user_schedules.order(:date_time).group_by { |sched| sched.date_time.beginning_of_day }
 @users_schedules_shared = User.find_by(:email) #HERE I NEED THE USER WHICH BELONGS_TO THIS SCHEDULE
    end
  end
end


我希望你能理解我的问题 感谢您的帮助!

<小时/> 修改
我收集控制器中的所有数据:

class WelcomeController < ApplicationController

  def index
    if(current_user)
      @user_schedules = current_user.schedules
      @user_schedules_date = @user_schedules.order(:date_time).group_by { |sched| sched.date_time.beginning_of_day }
      @users_all = User.includes(user_schedules: :schedules)
    end
  end
end

并按以下方式编辑视图:

<% @users_all.each do |user| %>
  <% user.name %>
<% end %>

但是我收到以下错误:
Association named 'user_schedules' was not found on User; perhaps you misspelled it?

我红了this,正如 deyan 所说,但我不明白
@users_all = User.includes(user_schedules: :schedules)&lt; - 返回一个数组?!?? (如果我理解正确的话)
所以我需要每个array-item.name来显示用户名?
谁能告诉我我做错了什么?
数据库:
enter image description here http://i.stack.imgur.com/Apg7y.png

  1. 用户
  2. schedules_users(与fk一起加入表格)
  3. 调度

2 个答案:

答案 0 :(得分:1)

  

我想向用户展示has_and_belongs_to_many时间表

这将使用称为many-to-many关系的东西 - 这意味着如果您通过模型访问关联数据 - 您将拥有一个附加属性/方法来捕获它们。

所以你要做的是以下几点:

#app/models/schedule.rb
Class Schedule < ActiveRecord::Base
   has_and_belongs_to_many :users
end

#app/models/user.rb
Class User < ActiveRecord::Base
   has_and_belongs_to_many :schedules
end

这将为每个模型对象附加collection,允许您根据需要调用集合:

@schedules = Schedule.all
@schedules.each do |schedule|
   schedule.users #-> outputs a collection
   schedule.users.each do |user|
       user.name
   end
end

includes与Rails一起使用实际上是一件非常糟糕的事情,考虑到你可以调用ActiveRecord关联来为你做繁重的工作

-

has_and_belongs_to_many

简单地说,您无法直接访问has_and_belongs_to_many表,因为它没有primary_keys

enter image description here

Rails基本上使用relational database基础结构(通过ActiveRecord)来访问关联数据。这意味着,如果您使用此特定类型的表格,那么您只能访问collection条款:

#app/models/user.rb
Class User < ActiveRecord::Base
   has_and_belongs_to_many :schedules
end

#app/models/schedule.rb
Class Schedule < ActiveRecord::Base
   has_and_belongs_to_many :users
end

这将允许您访问:

@user = User.find 1
@user.schedules #-> shows schedules collection. You will have to loop through this

答案 1 :(得分:0)

如果我理解正确,您希望显示当前用户的日程安排,以及每个日程安排以显示其他用户所属的日程安排。

  1. 根据您的观点,为了让所有用户都能获得日程安排,您只需要将schedules.users_belongs_to替换为schedule.users

  2. 如果您希望将当前用户可能拥有的所有计划的所有唯一身份用户聚集在一起,那么您可以在控制器中current_user.schedules.collect{|s| s.users|}.uniq。这仅在您想要显示所有用户时才有用,无论每个用户属于哪个计划。

  3. 我建议您收集控制器中的所有数据,然后在视图中打印。您可以通过一次调用DB来加入所有表,这可能看起来像这样:User.includes(users_schedules: :schedules)但您需要根据模型的调用来调整它。

  4. 方法1或2使用您当前的代码解决您的问题,但可能很慢。我建议你在这里阅读更多内容:http://guides.rubyonrails.org/active_record_querying.html并立即获取所有数据(方法3)。