此行将引发错误“参数数量错误(给定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
您在查询中遇到的错误是由于all方法不需要任何参数,它只是检索给定模型/关系的所有记录。
在这种情况下,您要使用的是ActiveRecord :: QueryMethods中的where方法。
还有另一个错误,您在您的条件下使用单数形式的表名,该表名应为复数形式(用课程代替课程)。
另外,您可以在此处使用includes方法和references方法结合使用以生成数据库联接。
因此,您将获得如下所示的内容:
@posts = Post.includes(:course).where("courses.name IN (#{@user.courses.map(&:name).collect { |s| '#{s}' }.join(',') })").references(:courses).order("posts.created_at DESC")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句