Just create the relationship too much for many in Ruby

Asked

Viewed 137 times

1

I have a very long-standing relationship with Yell and Category entities:

model Yell.Rb

class Yell < ActiveRecord::Base
  belongs_to :user, inverse_of: :yells
  has_and_belongs_to_many :categories

  def as_json(options={})
    super(:only => [:id, :title, :desciption, :price, :payment_type, :yell_type],
          :include => {
              :categories => {:only => [:id, :name]},
              :user => {:only => [:id, :name]}
          }
    )
  end
end

model Category.Rb

class Category < ActiveRecord::Base
  has_and_belongs_to_many :yells

  def as_json(options={})
    super(:only => [:id, :name],
          :include => {
              :yells => {:only => [:id, :title]}
          }
    )
  end
end

migrate create_categories_yells_join_table.Rb

class CreateCategoriesYellsJoinTable < ActiveRecord::Migration
  def self.up
    create_table :categories_yells, :id => false do |t|
      t.belongs_to :category, index: true
      t.belongs_to :yell, index: true
    end
  end

  def self.down
    drop_table :categories_yells
  end
end

With that I made a method crete where he will create my yell and create the category if it does not exist and creates the relationship between the two. And if the category already exists is only to create the relationship.

Then I make the following call POST:

http://localhost:3000/api/yells
{
    "user_id":"1",
    "title":"mouse",
    "desciption":"rato",
    "yell_type":"demanda",
    "price":"20,00",
    "categories":[{"name":"tecnologica"}]
}

and he calls my method create in controller_yells.Rb:

def create
  #@yell = Yell.new(yell_params.except(:categories))
  @yell = Yell.new({title: params[:title], desciption: params[:desciption], price: params[:price], user_id: params[:user_id], yell_type: params[:yell_type]})

  Array(params[:categories]).each do |rel|
    @category = Category.find_by_name(rel[:name])
    if @category
      #cria apena o relacionamento entre a category e o yell
    else
      @yell.categories.build(name: rel[:name]) #creates the relationship and category
    end
  end

  if @yell.save
    render json: {status: 0, message:"sucess", data: @yell}, status: :created
  else
    render json: @yell.errors, status: :unprocessable_entity
  end
end

he creates quiet everything straight, but do not know what to put in place of the comment #cria apena o relacionamento entre a category e o yell.

Does anyone know how to do this or some more elegant solution?

1 answer

0


To do this I had to create a model for my Join table:

model category_yell.Rb

class CategoriesYell < ActiveRecord::Base
  belongs_to :category
  belongs_to :yell
end

and my create method was as follows:

def create
  #@yell = Yell.new(yell_params.except(:categories))
  @yell = Yell.new({title: params[:title], desciption: params[:desciption], price: params[:price], user_id: params[:user_id], yell_type: params[:yell_type]})

  if @yell.save
    Array(params[:categories]).each do |rel|

      @category = Category.find_by_name(rel[:name])

      if @category
        @categories_yells = CategoriesYell.new(category_id: @category.id, yell_id: @yell.id)

        if @categories_yells.save
          @yell.categories.build(id: @category.id, name: rel[:name])#only creates the relationship
        else
          render json: {status: 1, message:"relationship categoy not create", data: @yell.errors}, status: :unprocessable_entity
        end

      else
        @yell.categories.create(name: rel[:name]) #creates the relationship and category
      end

    end

    render json: {status: 0, message:"sucess", data: @yell}, status: :created
  else
    render json: {status: -1, message:"error", data: @yell.errors}, status: :unprocessable_entity
  end

end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.