使用“has_many :through”时出现未定义方法错误Rails3 上的关系

发布于 2024-10-31 08:13:06 字数 2175 浏览 1 评论 0原文

我正在开发一个带有 RoR 服务器的小型 Android 项目。 以下是三个模型:

class User < ActiveRecord::Base
      has_many :relations
      has_many :friends, :through => :relations
      attr_accessor :friend_ids
end

class Relation < ActiveRecord::Base
   belongs_to :user
   belongs_to :friend
end

class Friend < ActiveRecord::Base
   has_many :relations
   has_many :users, :through => :relations
end


class CreateUsers < ActiveRecord::Migration
 def self.up
create_table :users do |t|
  t.string :user_name
  t.string :password
  t.integer :city_id
  t.integer :travelstyle_id
  t.boolean :online
  t.string :self_description
  t.string :sex
  t.integer :head_id

  t.timestamps
end

end

def self.down
  drop_table :users
end

end

 class CreateFriends < ActiveRecord::Migration
 def self.up
create_table :friends do |t|
  t.string :user_name
  t.integer :city_id
  t.integer :travelstyle_id
  t.string :self_description
  t.string :sex
  t.integer :head_id
  t.timestamps
end

end

  class CreateRelations < ActiveRecord::Migration
def self.up
create_table :relations do |t|
  t.integer :user_id
  t.integer :friend_id
  t.timestamps
end

end

模型用户使用模型关系与模型朋友连接。我使用脚手架创建三个模型,并在它们的模型文件中添加关系代码。我还创建了一个 API 控制器来将 xml 文件发送到 Android 应用程序。这是控制器代码:

def find_friend
  @user=User.where("id=?",params[:id])
  @[email protected]
respond_to do |format|
  format.html
  format.xml
end

end

这是问题,当我使用 api 时(输入 http:// /localhost:3000/api/find_friend/1.xml),服务器抛出一个错误:

NoMethodError in ApiController#find_friend

undefined method `friends' for #<ActiveRecord::Relation:0x3478a28>
app/controllers/api_controller.rb:21:in `find_friend'

我是 Rails 新手,不知道错误出在哪里。我是否必须在“route.rb”中添加某些内容或更改迁移文件?

在rails控制台模式下,我输入“user=User.find(1),friend=user.friends”并得到正确的结果。

I am working on a small Android project with RoR server.
Here are the three models:

class User < ActiveRecord::Base
      has_many :relations
      has_many :friends, :through => :relations
      attr_accessor :friend_ids
end

class Relation < ActiveRecord::Base
   belongs_to :user
   belongs_to :friend
end

class Friend < ActiveRecord::Base
   has_many :relations
   has_many :users, :through => :relations
end


class CreateUsers < ActiveRecord::Migration
 def self.up
create_table :users do |t|
  t.string :user_name
  t.string :password
  t.integer :city_id
  t.integer :travelstyle_id
  t.boolean :online
  t.string :self_description
  t.string :sex
  t.integer :head_id

  t.timestamps
end

end

def self.down
  drop_table :users
end

end

 class CreateFriends < ActiveRecord::Migration
 def self.up
create_table :friends do |t|
  t.string :user_name
  t.integer :city_id
  t.integer :travelstyle_id
  t.string :self_description
  t.string :sex
  t.integer :head_id
  t.timestamps
end

end

  class CreateRelations < ActiveRecord::Migration
def self.up
create_table :relations do |t|
  t.integer :user_id
  t.integer :friend_id
  t.timestamps
end

end

Model User uses model Relation to connect with model Friend. I use scaffold to creat the three models and add the relationship code in their model files. I also create a API controller to send xml file to Android application. Here is the controller code:

def find_friend
  @user=User.where("id=?",params[:id])
  @[email protected]
respond_to do |format|
  format.html
  format.xml
end

end

Here is the problem, when I use the api(type in http://localhost:3000/api/find_friend/1.xml), the server throws a mistake:

NoMethodError in ApiController#find_friend

undefined method `friends' for #<ActiveRecord::Relation:0x3478a28>
app/controllers/api_controller.rb:21:in `find_friend'

I am new to Rails and have no idea where the wrong is. Do I have to add something in the "route.rb" or change the migration file?

In the rails console mode, I type in "user=User.find(1), friend=user.friends" and get the correct result.

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

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

发布评论

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

评论(1

酒废 2024-11-07 08:13:06

~~~~(>_<)~~~~
问题是控制器方法“@user=User.where("id=?",params[:id])”。 “where”方法无法判断结果是一个数组还是实际上一个对象。如果我使用“@user=User.find(params[:id])”,rails将“足够聪明”知道“哦,是的,这只是一个对象,它有一个名为 Friends 的方法,因为有人连接了两个模型在一起”。
学习Rails就像婚姻,你以为你很了解她,但有时你会想“天哪,我对这个神秘的家伙一无所知”。

~~~~(>_<)~~~~
The problem is the controller method "@user=User.where("id=?",params[:id])". The "where" method can not tell whether the result is an array or actually one object. If I use "@user=User.find(params[:id])", rails will be "smart enough" to know that "Oh, yes, this is just one object and it has a method called Friends because someone connects the two models together".
Learning Rails likes a marriage, you think you know well about her but sometimes you think "God actually I know nothing about the mysterious guy."

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