Rails 2.3.x 中通过关联过滤记录有很多

发布于 2024-10-21 08:38:45 字数 1591 浏览 1 评论 0原文

我有三个模型通过 has_many_through 关联连接。

class Board < ActiveRecord::Base
has_many   :celebrations, :dependent => :destroy
has_many   :users, :through => :celebrations

class User < ActiveRecord::Base
has_many :boards,
         :through => :celebrations
has_many :celebrations, :dependent => :destroy

class Celebration < ActiveRecord::Base
belongs_to :user
belongs_to :board

class CreateCelebrations < ActiveRecord::Migration
  def self.up
   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.timestamps
      end
  end

我想获取特定论坛的所有用户,其中用户的角色是朋友。该角色位于 Celebrations 表中。

我已在控制器中尝试了以下操作:

@friends = User.condtions(:celebrations => {:role => "FRIEND", :board_id => session[:board_id]})

其结果是:

NoMethodError in FriendsController#show

undefined method `condtions' for #<Class:0x1023e3688>

我已尝试:

@friends = Board.find(session[:board_id]).celebrations.conditions(:role => "FRIEND").joins(:user)

其结果是:

ArgumentError in FriendsController#show

wrong number of arguments (1 for 0)

如何获取具有特定板的好友角色的用户?

预先非常感谢您。

这有效:

board = Board.find(session[:board_id])
@friends = board.users.find(:all, :conditions => ["celebrations.role = ?", "FRIEND"])

I have three models connected via an has_many_through association.

class Board < ActiveRecord::Base
has_many   :celebrations, :dependent => :destroy
has_many   :users, :through => :celebrations

class User < ActiveRecord::Base
has_many :boards,
         :through => :celebrations
has_many :celebrations, :dependent => :destroy

class Celebration < ActiveRecord::Base
belongs_to :user
belongs_to :board

class CreateCelebrations < ActiveRecord::Migration
  def self.up
   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.timestamps
      end
  end

I would like to get all of the users for a specific board where the role of the user is FRIEND. The role is in the Celebrations table.

I have tried the following in the controller:

@friends = User.condtions(:celebrations => {:role => "FRIEND", :board_id => session[:board_id]})

which results in:

NoMethodError in FriendsController#show

undefined method `condtions' for #<Class:0x1023e3688>

I have tried:

@friends = Board.find(session[:board_id]).celebrations.conditions(:role => "FRIEND").joins(:user)

which results in:

ArgumentError in FriendsController#show

wrong number of arguments (1 for 0)

How can I get the users who have the rols of FRIENDS for a specific board?

Thank you very much in advance.

This works:

board = Board.find(session[:board_id])
@friends = board.users.find(:all, :conditions => ["celebrations.role = ?", "FRIEND"])

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

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

发布评论

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

评论(1

原来是傀儡 2024-10-28 08:38:45

我在 Board.rb 文件中扩展了类关联。

has_many   :users, :through => :celebrations do
           def by_role(role) #great for returning all of the users whose role is FRIEND
               find(:all, :conditions => ["celebrations.role = ?", role])
               end
           end

然后我可以在我的控制器中调用以下内容。

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

感谢 Josh Susser 和他的博客

I extended the class association in the Board.rb file.

has_many   :users, :through => :celebrations do
           def by_role(role) #great for returning all of the users whose role is FRIEND
               find(:all, :conditions => ["celebrations.role = ?", role])
               end
           end

And then I can call the following in my controller.

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

Thank to Josh Susser and his blog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文