0

У меня есть три модели в контексте этого вопроса:Как написать активную запись запроса

class ClearanceBatch < ActiveRecord::Base  
    has_many :items 
    belongs_to :user 
end 

class Item < ActiveRecord::Base 

    belongs_to :style 
    belongs_to :user 
    belongs_to :clearance_batch 
    validates :id, :uniqueness => true 
end 

class User < ActiveRecord::Base 

    has_many :items, dependent: :destroy 
    has_many :clearance_batches, dependent: :destroy  
    enum role: {staff: 0, vendor: 1, admin: 2}   

end 

Схема:

create_table "clearance_batches", force: :cascade do |t| 
     t.datetime "created_at" 
     t.datetime "updated_at" 
     t.boolean "status",  default: false 
     t.string "boughtby", default: "" 
     t.integer "user_id" 
     end 

     add_index "clearance_batches", ["user_id"], name: "index_clearance_batches_on_user_id" 

     create_table "items", force: :cascade do |t| 
     t.string "size" 
     t.string "color" 
     t.string "status" 
     t.decimal "price_sold" 
     t.datetime "sold_at" 
     t.integer "style_id" 
     t.datetime "created_at" 
     t.datetime "updated_at" 
     t.integer "clearance_batch_id" 
     t.integer "user_id" 
     end 



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.integer "role",     default: 0 
     end 

Я хочу, чтобы найти все элементы в партии, вошедшие в пользователь (главным образом поставщик) со статусом «очищенный» и получить их данные в цикле от контроллера до моего вида

Может ли кто-нибудь помочь мне с активным запросом на запись? Пожалуйста! :)

SQLite запросов Я думаю, что было бы:

Select I.id from clearance_batches C INNER JOINS Items I on C.id = I.clearance_batch_id where C.user_id = "1" and I.status = "clearanced" 

(Если 1 является текущий пользователь, имея в виду, я только что позволяет пользователю поставщиков ролей быть пользователем в clearance_batch таблице)

ответ

0

(1) Запрос:

Items.where(status: "clearanced") 
    .joins(:clearance_batches) 
    .where(clearance_batches: {user_id: current_user}) 

(2) контроллер:

@clearanced_items = query(1) 

(3) Вид:

<% @clearanced_items.each do |c_item| %> 
    ... 
<% end %> 
Смежные вопросы