使用“has_many :through”时出现未定义方法错误Rails3 上的关系
我正在开发一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
~~~~(>_<)~~~~
问题是控制器方法“@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."