A
A
Anton Ipatov2013-12-16 16:29:42
Ruby on Rails
Anton Ipatov, 2013-12-16 16:29:42

What is the correct way to add categories to Ruby on Rails?

Hello, how to add categories to projects correctly
? I need to be able to select a category when creating a project, and then sort projects by category.
At the moment, I did this:

class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.string :name
      t.integer :user_id

      t.timestamps
    end
    add_index :categories, :user_id
  end
end

class CreateProjects < ActiveRecord::Migration
  def change
    create_table :projects do |t|
      t.integer :user_id
      t.string :title
      t.text :description
      t.belongs_to :category

      t.timestamps
    end
    add_index :projects, [:user_id, :category_id, :created_at]
  end
end

class Category < ActiveRecord::Base
  attr_accessible :name

  has_many :products

  belongs_to :user

  validates :user_id, presence: true
end

class Project < ActiveRecord::Base
  attr_accessible :description, :title, :category_id

  belongs_to :user, touch: true  
  belongs_to :category

  validates :user_id, presence: true
  validates :category_id, presence: true
end

<%= form_for(@project) do |f| %>
  <fieldset>
    <%= f.label :Title %>
    <%= f.text_field :title, class: 'span12' %>

    <%= f.label :Description %>
    <%= f.text_area :description, class: 'span12' %>


    <%= f.label :category_id %>
    <%= f.collection_select :category_id, Category.order(:name), :id, :name, class: 'span12' %>

    <%= f.submit 'Save', disable_with: '...', class: 'btn btn-default' %>
  </fieldset>
<% end %>

How correct is this and how to sort projects into categories?
I would be very grateful if you help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Nesterovich, 2013-12-16
@IpatovAnton

Quite normal.
To sort, add a form (to the index) with a select:
In control make a selection:
or

Category.includes(:projects).find(params[:category_id])

M
medium3001, 2014-08-15
@medium3001

Tell me, I did it almost exactly according to this scheme, the category is selected, but the trouble is with the display of the selected category in the post. category_id is NULL in the database. like nothing is stored there.

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title
      t.text :text
      t.belongs_to :category, index: true
      t.timestamps
    end

    create_table :categories do |t|
      t.string :name
      t.timestamps
    end
  end
end

class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  belongs_to :category
  validates :title, presence: true,
                    length: { minimum: 5 }

end

class Category < ActiveRecord::Base
  has_many :articles
end

in index file
<td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= article.category_id %></td>

category_id not showing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question