简体   繁体   中英

Find filtered record with Ember.js and Rails

I'm trying to add a "where" filter in my Rails backend doing so :

  def index
    @label = Label
    if params[:filter].present?
      @label =  @label.where(uid: params[:filter])
    end

    @label = @label.all

    render json: @label
  end

The problem is that I get this SQL request:

  Label Load (1.0ms)  SELECT "labels".* FROM "labels" WHERE "uid"."uid" = 'VALUE'

And "uid"."uid" throws an error

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: uid.uid: SELECT "labels".* FROM "labels" WHERE "uid"."uid" = 'VALUE'):

app/controllers/labels_controller.rb:13:in `index'

EDIT

Here my label schema:

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

Do you know why it doens't use "labels"."uid" ?

Can't explain why it didn't work, but here my solution :

  def index
    @label = Label

    if params[:filter].present?
      @label = @label.where("uid = ?", params[:filter][:uid])
    end

    @label = @label.all

    render json: @label
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM