jeudi 2 février 2017

Converting SQL Raw query to Rails. Relationship issue

I'am trying to figure it out how to convert this SQL query to Active record.

SELECT url_picture
FROM ingredients, ingredients_tutorials_steps, ingredients_tutorials, steps, subtypes
WHERE #{object.id} = ingredients_tutorials_steps.step_id
AND ingredients_tutorials_steps.ingredients_tutorial_id = ingredients_tutorials.id;"

Object.id is a STEP_ID obviously.

I'am trying to get the url_picture stored in 'subtype' for each STEP.

I have those relationships in place :

Ingredients.rb ----

  belongs_to :subtype
  has_many :ingredients_users, dependent: :destroy

Ingredients_tutorial.rb ----

  belongs_to :ingredient
  belongs_to :tutorial
  has_many :ingredients_tutorials_steps, dependent: :destroy
  delegate :title, to: :ingredient
  delegate :type, to: :ingredient
  delegate :file_url, to: :ingredient
  delegate :url_picture, to: :ingredient
  delegate :subtype, to: :ingredient

Ingredients_Tutorials_Step.rb ----

  belongs_to :ingredients_tutorial
  belongs_to :step
  delegate :title, to: :ingredients_tutorial
  delegate :type, to: :ingredients_tutorial
  delegate :file_url, to: :ingredients_tutorial
  delegate :url_picture, to: :ingredients_tutorial
  delegate :subtype, to: :ingredients_tutorial

Step.rb ----

  belongs_to :chapter

  has_many :ingredients_tutorials_steps, dependent: :destroy, inverse_of: :step
  has_many :ingredients_tutorials, through: :ingredients_tutorials_steps
  has_many :images, dependent: :destroy, inverse_of: :step
  has_many :step_comments, dependent: :destroy
  has_many :bookmarks, dependent: :destroy
  has_many :cards, dependent: :destroy, inverse_of: :step
  has_many :steps_users, dependent: :destroy, inverse_of: :step
  has_one :video, dependent: :destroy, inverse_of: :step
  has_one :audio, dependent: :destroy, inverse_of: :step
  accepts_nested_attributes_for :ingredients_tutorials_steps, allow_destroy: :true

Aucun commentaire:

Enregistrer un commentaire