0
此行引发错误“参数的错误数量(给定1,预期0)”。我真的很想知道如何让这个查询工作。谢谢!为什么我在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
这把错误远离查询,这是伟大的!但是我现在在“@ posts.each do”上有一个错误,说“在Post上没有找到'Association''courses'';” – Ryan
这是在includes中使用的关联名称的问题(在引用中它是正确的,因为此方法需要表名!),它应该是课程而不是课程,因为它是一对多(一对一职位)关系。我已修复了错字。此外,我无法验证整个查询,因为我在手机上。 – joaovictortr
有这个错误:SQLite3 :: SQLException:在“网络”附近:语法错误:SELECT“posts”。“id”AS t0_r0,“posts”。“title”AS t0_r1,“posts”。“content”AS t0_r2, “posts”。“created_at”AS t0_r3,“posts”。“updated_at”AS t0_r4,“posts”。“user_id”AS t0_r5,“posts”。“course_id”AS t0_r6,“courses”。“id”AS t1_r0, “courses”。“name”AS t1_r1,“courses”。“created_at”AS t1_r2,“courses”。“updated_at”AS t1_r3,“courses”。“major_id”AS t1_r4,“courses”。“user_id”AS t1_r5 FROM “posts”LEFT OUTER JOIN“courses”ON“courses”。“id”=“posts”。“course_id”WHERE(courses.name IN(Electrical Networks,Physics 3)) – Ryan