2016-07-27 1 views
0

이 줄은 "잘못된 인수 수 (주어진 1, 예상 0)"를 발생시킵니다. 이 쿼리를 작동시키는 방법을 알고 싶습니다. 감사! 여기Rails의 검색 쿼리에 대해 "잘못된 인수 (주어진 1, 예상 0)"오류가 발생합니까?

@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 

답변

0

오류입니다

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

이 내 컨트롤러 코드는 쿼리에서 all 메서드는 매개 변수를 필요로하지 않고 지정된 모델/관계에 대한 모든 레코드를 검색합니다.

이 경우 사용하려는 것은 ActiveRecord :: QueryMethods의 where 메서드입니다.

또 다른 오류가 있습니다. 단 하나의 조건으로 테이블의 이름을 사용하고 있습니다 (단, 물론 코스가 아닌 복수형이어야합니다).

또한 여기에서 메서드와 결합 된 includes 메서드를 사용하여 데이터베이스 조인을 생성 할 수 있습니다.

그래서, 당신은 다음과 같은 일을 할 것이다 :

@posts = Post.includes(:course).where("courses.name IN (#{@user.courses.map(&:name).collect { |s| '#{s}' }.join(',') })").references(:courses).order("posts.created_at DESC") 
+0

이 대단한 쿼리에서 떨어진 오류를했다! 하지만 이제는 "@ posts.each do"에 "Post라는 이름의 'Association'courses '를 찾을 수 없다는 오류가 있습니다." – Ryan

+0

이것은 포함에 사용 된 연관 이름에 문제가 있습니다 (이 메소드는 테이블 이름을 필요로하기 때문에 올바른 참조로!), 코스가 아닌 코스가되어야합니다. 일대 다 (one to many) 게시물) 관계. 오타가 수정되었습니다. 또한 모바일에서와 마찬가지로 아직 쿼리를 완전히 검증 할 수 없습니다. – joaovictortr

+0

오류 : SQLite3 :: SQLException : "네트워크"근처 : 구문 오류 : SELECT "posts". "id"AS t0_r0, "posts". "title"AS t0_r1, "posts". "content"AS t0_r2, "posts". "posts". "course_id"AS t0_r6, "courses". "id"AS t1_r0, "posts". "created_at"AS t0_r3 "게시물". "updated_at"AS t0_r4 "게시물". "user_id"AS t0_r5 " "courses" "이름"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

관련 문제