通过关联 Rails 指定具有多个的列

发布于 2024-10-24 20:08:04 字数 1330 浏览 1 评论 0原文

我有三个模型。

User
has_many :boards through => :celebrations
has_many   :celebrations, :dependent => :destroy

Board
has_many   :celebrations, :dependent => :destroy
has_many  :users, :through => :celebrations do
           def by_role(role) 
           find(:all, :conditions => ["celebrations.role = ?", role])
           end
           end

Celebration
:belongs_to :user
:belongs_to :board

庆祝表

   create_table :celebrations do |t|
      t.column :board_id,        :int, :null => false
      t.column :user_id,         :int, :null => false 
      t.column :role,            :string,  :null => false
      t.column :token,           :string
      t.column :accepted,    :boolean, :default => false
      t.timestamps

在控制器中:

  @board = Board.find(session[:board_id])
    @friends = @board.users.by_role("FRIEND")

在视图中:

<% for friend in @friends do %>
<%= friend.name %>
<%= friend.email %>

但是,当我执行以下操作时:

<% friend.celebration.accepted %> 

我收到以下错误:

undefined method `celebration' for #<User:0x104788c00>

如何使用模型扩展“by_role(role) 访问与记录一起返回的庆祝表中的列“已接受” ”。

提前感谢您的帮助。

I have three models.

User
has_many :boards through => :celebrations
has_many   :celebrations, :dependent => :destroy

Board
has_many   :celebrations, :dependent => :destroy
has_many  :users, :through => :celebrations do
           def by_role(role) 
           find(:all, :conditions => ["celebrations.role = ?", role])
           end
           end

Celebration
:belongs_to :user
:belongs_to :board

Celebration Table

   create_table :celebrations do |t|
      t.column :board_id,        :int, :null => false
      t.column :user_id,         :int, :null => false 
      t.column :role,            :string,  :null => false
      t.column :token,           :string
      t.column :accepted,    :boolean, :default => false
      t.timestamps

In the controller:

  @board = Board.find(session[:board_id])
    @friends = @board.users.by_role("FRIEND")

In the view:

<% for friend in @friends do %>
<%= friend.name %>
<%= friend.email %>

However when I do the following:

<% friend.celebration.accepted %> 

I get the following error:

undefined method `celebration' for #<User:0x104788c00>

How can I access the column 'accepted' in the celebrations table returned along with the record using the model extension "by_role(role)".

Thank you for your help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-10-31 20:08:04

您可以像这样急切地加载庆祝活动:

Board model:

Board
  has_many   :celebrations, :dependent => :destroy
  has_many  :users, :through => :celebrations do
     def by_role(role) 
       find(:all, :conditions => ["celebrations.role = ?", role], :include => :celebrations)
     end
  end

使用 :include 您将再获得一次点击:

Celebration Load (0.2ms) SELECT celebrations.* FROM celebrations WHERE (celebrations.user_id IN (5,6)) 

为用户加载庆祝活动数据。
在这种特殊情况下,这将避免检索每个用户的庆祝活动所需的两次点击。如果您有 10 个用户,您将以 1 个命中结束,而不是 10 个。

预加载会加载您需要循环访问的所有数据,与一个查询而不是 n 个查询相关联。

对于已接受的数据,问题仍然存在,因为由于与用户的 has_many 关系,您将有一系列庆祝活动。

你可以使用这样的东西:

<% friend.celebrations.map{|celeb| celeb.accepted }.join(",") %>

You can eager load the celebrations like this:

Board model:

Board
  has_many   :celebrations, :dependent => :destroy
  has_many  :users, :through => :celebrations do
     def by_role(role) 
       find(:all, :conditions => ["celebrations.role = ?", role], :include => :celebrations)
     end
  end

With :include you will have one more hit:

Celebration Load (0.2ms) SELECT celebrations.* FROM celebrations WHERE (celebrations.user_id IN (5,6)) 

that loads the celebrations data for users.
This will avoid in this particular case two hits that would be needed to retrieve celebrations for each user. If you have 10 users you will end with one hit instead of 10.

Eager loading loads all the data you will need to cycle through in association with one query instead of n queries.

The problem remains for the accepted data because you will have an array of celebrations due to the has_many relationship with user.

You can use something like this:

<% friend.celebrations.map{|celeb| celeb.accepted }.join(",") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文