5

Event と呼ばれるモデルと Product と呼ばれる別のモデルがあります。イベントには多くの製品があり、製品には多くのイベントがあります ( と呼ばれる結合モデルを介してEventproduct)。現在の日付範囲が別のイベントの日付範囲と一致するイベントに含まれていないすべての製品を選択するクエリを設計しようとしているため、ユーザーが日付範囲でイベントを作成すると、利用可能な製品が表示されるため、同一商品の同時出展はできません。これはアクティブ レコード クエリ インターフェイスで可能ですか、それとも独自の SQL クエリを作成する必要がありますか。

私の移行は次のようになります。

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :make
      t.string :model
      t.integer :wattage
      t.boolean :dmx
      t.decimal :price
      t.timestamps
    end
  end
end


class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.datetime :start_date
      t.datetime :end_date
      t.timestamps
    end
  end
end


class AddContactToEvent < ActiveRecord::Migration
  def change
    add_column :events, :name, :string
    add_column :events, :location, :string
    add_column :events, :contact_number, :string
  end
end

class CreateEventproducts < ActiveRecord::Migration
  def change
    create_table :eventproducts do |t|
      t.references :product
      t.references :event

      t.timestamps
    end
    add_index :eventproducts, :product_id
    add_index :eventproducts, :event_id
  end
end

関連するモデルは次のとおりです。

class Event < ActiveRecord::Base
  attr_accessible :end_date, :start_date, :products, :lightings, :name, :location, :contact_number, :product_ids
  has_many :products, :through => :Eventproduct
  has_many :Eventproduct
  validates_presence_of :name, :message => "can't be blank"
  validates_presence_of :location, :message => "can't be blank"
  validates_presence_of :contact_number, :message => "A telephone number is needed so that we can contact you if we have any problems"
  validates_presence_of :start_date, :message => "can't be blank"
  validates_presence_of :end_date, :message => "can't be blank"
end

class Eventproduct < ActiveRecord::Base
  belongs_to :product
  belongs_to :event
  # attr_accessible :title, :body
end


class Product < ActiveRecord::Base
  validates :price, numericality: {greater_than_or_equal_to: 0.01}
    attr_accessible :make, :model, :wattage, :dmx, :price
end
4

2 に答える 2

4

これを試してください: Product.includes(:Eventproduct).where(eventproducts: { event_id: nil }).group('products.id').

where条件内のテーブルの名前であることに注意してください。また、Eventproduct の関連付けを Product モデルに追加することを忘れないでください。has_many :Eventproduct

于 2012-07-02T13:53:23.187 に答える
4

私はあなたを助けることができるクエリを見つけました。時間範囲の条件とそのロジックを考え出す必要があります。

クエリは次のようになります

Product.joins(:events).where("events.start_date <= :start_date", {start_date: Time.now})

where 句には、不要なイベントをフィルター処理するためのロジックを含める必要があります。繰り返しますが、そのコードで作業を開始できます。あなたの質問に答えるために、それは可能です。返されたクエリを見て、それを回避して、ニーズに合った条件を作成します。また、私が行った方法で where 句を変更するのに役立つこのリンクを見てください: http://guides.rubyonrails.org/active_record_querying.html

これがお役に立てば幸いです!

アップデート:

製品の EventProduct テーブルにイベントがない場合、そのクエリは空を返すため、イベントがまったくない製品を含めるために、Product.all と一緒にいくつかのセットの違いを実行する必要がある場合があります。効率的ではないかもしれませんが、必要なものに応じて機能するはずです。

Product.all - Product.joins(:events).where("condition reversed")

これにより、まだイベントがないものを含め、条件を満たさないすべての製品が返されます。

于 2012-07-02T14:51:29.103 に答える