Rails 3.0.3 - ActiveRecord::Relation,未定义的方法错误

发布于 2024-10-09 12:38:46 字数 1816 浏览 8 评论 0原文

我遇到了这个无法解释的 ActiveRecord::Relation, undefined method error 。我不知道为什么,因为我的模型关联定义良好,并且事件表具有用户表的外键。我尝试使用此修复程序,但失败了: Rails 3 ActiveRecord::Relation 随机关联行为

event.rb

class Event < ActiveRecord::Base
  belongs_to :user

  attr_accessible :event_name, :Starts_at, :finish, :tracks
end

user.rb

class User < ActiveRecord::Base
  has_many :events, :dependent => :destroy

  attr_accessible :name, :event_attributes

  accepts_nested_attributes_for :events,  :allow_destroy => true

end

schema.rb

ActiveRecord::Schema.define(:version => 20101201180355) do

  create_table "events", :force => true do |t|

    t.string   "event_name"
    t.string   "tracks"
    t.datetime "starts_at"
    t.datetime "finish"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
  end
end

错误消息

NoMethodError in Users#index 
undefined method `events' for #<ActiveRecord::Relation:0x4177518>

Extracted source (around line #10):

7:  <th><%= sortable "Tracks" %></th>

8:   </tr>


10:  <% @users.events.each do |event| %> 

11: <% debugger %> 

12:   <tr>

13:     <td><%= event.starts_at %></td>

Trace of template inclusion: app/views/users/index.html.erb

Rails.root: C:/rails_project1/events_manager
Application Trace | Framework Trace | Full Trace

app/views/users/_event_user.html.erb:10:in `_app_views_users__event_user_html_erb__412443848_34308540_1390678'
app/views/users/index.html.erb:7:in `_app_views_users_index_html_erb___603337143_34316016_0'

I am having this unexplained ActiveRecord::Relation, undefined method error . I don't know why, since my model association are well defined and the event table has the foreign keys for the user table. I tried using this fix but it failed: Rails 3 ActiveRecord::Relation random associations behavior

event.rb

class Event < ActiveRecord::Base
  belongs_to :user

  attr_accessible :event_name, :Starts_at, :finish, :tracks
end

user.rb

class User < ActiveRecord::Base
  has_many :events, :dependent => :destroy

  attr_accessible :name, :event_attributes

  accepts_nested_attributes_for :events,  :allow_destroy => true

end

schema.rb

ActiveRecord::Schema.define(:version => 20101201180355) do

  create_table "events", :force => true do |t|

    t.string   "event_name"
    t.string   "tracks"
    t.datetime "starts_at"
    t.datetime "finish"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
  end
end

error message

NoMethodError in Users#index 
undefined method `events' for #<ActiveRecord::Relation:0x4177518>

Extracted source (around line #10):

7:  <th><%= sortable "Tracks" %></th>

8:   </tr>


10:  <% @users.events.each do |event| %> 

11: <% debugger %> 

12:   <tr>

13:     <td><%= event.starts_at %></td>

Trace of template inclusion: app/views/users/index.html.erb

Rails.root: C:/rails_project1/events_manager
Application Trace | Framework Trace | Full Trace

app/views/users/_event_user.html.erb:10:in `_app_views_users__event_user_html_erb__412443848_34308540_1390678'
app/views/users/index.html.erb:7:in `_app_views_users_index_html_erb___603337143_34316016_0'

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

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

发布评论

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

评论(2

栀子花开つ 2024-10-16 12:38:46

如果您仔细阅读错误消息,它并没有说明问题在于与事件的关系。
它说:

未定义方法“事件”

10: <% @users.events.each do |event|
%>

当我第一次遇到它时,我也很难理解这一点。

这意味着 @users 的查找器返回一个 Relation 对象,而不是您期望的用户列表(或命名不正确的对象)。

如果您在任何地方使用查找,则应将其更改为“where(:id => ...).first”

例如,您的控制器中可能有类似以下内容的内容:

@users = User.find(<conditions go here>)

应更改为:

@users = User.where(<conditions go here>).all

或者,您可以在“where”之后使用生成的关系对象作为额外条件和sql“配置”。

@users = User.where(:admin => true).where('created_at > ?', min_date).order('created_at').limit(10).all

仅当调用“.first”、“.all”、“.each”或“.inspect”时,关系对象才会执行查询在它上面。

If you read the error message closely, it doesn't say the problem is the relation with the events.
It says:

undefined method `events' for

10: <% @users.events.each do |event|
%>

I also had trouble understanding this when i first bumped into it

This means that the finder of @users is returning a Relation object instead of the user list (or the not well named object) you expected.

If you are using a find anywhere, you should change it for a "where(:id => ...).first"

For example, you probably have something that looks like this in your controller:

@users = User.find(<conditions go here>)

This should be changed to:

@users = User.where(<conditions go here>).all

Or you could use the resulting relation object after the "where" for extra conditions and sql "configuration"

@users = User.where(:admin => true).where('created_at > ?', min_date).order('created_at').limit(10).all

A relation object will execute the query only when a ".first" ".all" ".each" or ".inspect" is invoked on it.

毅然前行 2024-10-16 12:38:46

一个用户有许多事件。在您的代码中,您似乎正在尝试访问 @users 上的事件,这可能是多个用户。

您可能想要执行类似以下操作:

@users.each do |user|
  user.events.each do |event|
    ...
  end
end

或:

 @user.map(&:events).flatten.each do |event|
   ...
 end

此外,您不需要在数据库中属于列的任何属性上指定 attr_accessible

One user has many events. In your code, it looks like you're trying to access events on @users, which presumably would be more than one user.

You probably want to do something like:

@users.each do |user|
  user.events.each do |event|
    ...
  end
end

or:

 @user.map(&:events).flatten.each do |event|
   ...
 end

Additionally, you don't need to specify attr_accessible on any attributes that are columns in your database.

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