通过关联 Rails 指定具有多个的列
我有三个模型。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样急切地加载庆祝活动:
Board model:
使用 :include 您将再获得一次点击:
为用户加载庆祝活动数据。
在这种特殊情况下,这将避免检索每个用户的庆祝活动所需的两次点击。如果您有 10 个用户,您将以 1 个命中结束,而不是 10 个。
预加载会加载您需要循环访问的所有数据,与一个查询而不是 n 个查询相关联。
对于已接受的数据,问题仍然存在,因为由于与用户的 has_many 关系,您将有一系列庆祝活动。
你可以使用这样的东西:
You can eager load the celebrations like this:
Board model:
With :include you will have one more hit:
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: