为什么我在Rails中为搜索查询得到错误的“错误的参数数量(给定1,预期为0)”?

时间:2016-07-27 02:46:42

标签: ruby-on-rails

此行引发错误“错误的参数数量(给定1,预期为0)”。我真的想知道如何使这个查询工作。谢谢!

@posts = Post.all(:joins => :course, :conditions => "course.name in (#{@user.courses.map(&:name).join(',')})",:order => "posts.created_at DESC")

这是我的控制器中的代码:

@user = current_user
@posts = Post.all(:joins => :course, :conditions => "course.name in (#{@user.courses.map(&:name).join(',')})",:order => "posts.created_at DESC")

以下是模型:

class Post < ActiveRecord::Base
belongs_to :user
belongs_to :course
has_many :comments
end

class Course < ActiveRecord::Base
belongs_to :user
has_many :posts
belongs_to :major
end

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
      :recoverable, :rememberable, :trackable, :validatable

has_many :courses
belongs_to :major
has_many :posts
has_many :comments

accepts_nested_attributes_for :courses, reject_if: :all_blank,      allow_destroy: true
end

这是架构

 create_table "comments", force: :cascade do |t|
t.text     "comment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer  "user_id"
t.integer  "post_id"
end

add_index "comments", ["post_id"], name: "index_comments_on_post_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"

create_table "courses", force: :cascade do |t|
t.string   "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer  "major_id"
t.integer  "user_id"
end

add_index "courses", ["major_id"], name: "index_courses_on_major_id"
add_index "courses", ["user_id"], name: "index_courses_on_user_id"

create_table "majors", force: :cascade do |t|
t.string   "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "posts", force: :cascade do |t|
t.string   "title"
t.text     "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer  "user_id"
t.integer  "course_id"
end

add_index "posts", ["course_id"], name: "index_posts_on_course_id"
add_index "posts", ["user_id"], name: "index_posts_on_user_id"

create_table "users", force: :cascade do |t|
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at",                          null: false
t.datetime "updated_at",                          null: false
t.boolean  "admin"
t.string   "username"
t.integer  "major_id"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["major_id"], name: "index_users_on_major_id"
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
add_index "users", ["username"], name: "index_users_on_username", unique: true

 end

1 个答案:

答案 0 :(得分:0)

您在查询中遇到的错误是由于all方法不期望任何参数,它只检索给定模型/关系的所有记录。

在这种情况下,您要使用的是ActiveRecord :: QueryMethods中的where方法。

还有另一个错误,你在你的条件下以单数形式使用表格的名称,它应该是复数(课程而不是课程)。

此外,您可以在此处使用includes方法与references方法相结合来生成数据库连接。

所以,你会得到以下内容:

{{1}}