has_many through associations - ruby-on-rails

I'm making an online fashion store. I have a Category Model and a Sizes Model. A model can have many sizes. A Size can have many Categories.
Therefore i am using has_many through association. The category model and sizes model will be linked via a table called category_sizes.
I will create a bunch of sizes like XS, Small, Medium. Then I will create a category Lets say Shirt then i can select all the Sizes a Shirt will have. Then click create.
How do i make the sizes appear in my view? I tried for hours.
Category Model
class Category < ActiveRecord::Base
has_ancestry
has_many :items
validates :name, presence: true, length: { maximum: 20 }
has_many :category_sizes
has_many :sizes, through: :category_sizes
end
Sizes Model
class Size < ActiveRecord::Base
validates :title, presence: true, length: { maximum: 15 }
validates :title, uniqueness: true
has_many :category_sizes
has_many :categories, through: :category_sizes
end
category_size model
class CategorySize < ActiveRecord::Base
belongs_to :category
belongs_to :sizes
end
Here is my form.
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_form_for(#category) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.association :category_sizes %>
<%= f.collection_select :parent_id, Category.order(:name), :id, :name, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
<div class="form-actions"><%= f.button :submit %></div>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
Schema
ActiveRecord::Schema.define(version: 20150915113019) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.string "ancestry"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "categories", ["ancestry"], name: "index_categories_on_ancestry"
create_table "category_sizes", force: :cascade do |t|
t.integer "category_id"
t.integer "size_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "category_sizes", ["category_id"], name: "index_category_sizes_on_category_id"
add_index "category_sizes", ["size_id"], name: "index_category_sizes_on_size_id"
create_table "items", force: :cascade do |t|
t.string "title"
t.decimal "price"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "category_id"
end
add_index "items", ["user_id", "created_at"], name: "index_items_on_user_id_and_created_at"
add_index "items", ["user_id"], name: "index_items_on_user_id"
create_table "sizes", force: :cascade do |t|
t.text "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "taggings", force: :cascade do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
create_table "users", force: :cascade do |t|
t.string "username"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.string ">"
t.datetime "reset_sent_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.text "description"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end

Did you try something like this?
<%= f.select(:size_id, Size.all.map {|s| [s.title, s.id]}) %>

In your controller define ,
#size_variant_array = #category.sizes
In your view,
<%= select_tag 'size',options_for_select(#size_variant_array.collect{ |pv| [pv.title, pv.id] }) ,:prompt => "Please Select"%>

Related

Rails has_many_through query not returning results

I have some models with a has_many_through join. I am trying to get a list of classification_fields and their properties where the classification_id is in the array I am pasing through. This following query doesn't seem to be getting anything. What am I doing wrong?
Get the parent ids and query the classificationfields:
#parentids = #classification.self_and_ancestors_ids.to_a if params[:class_id].present?
#details = ClassificationField.includes(:classifications).where(classification_id: [#parentids] ) if params[:sub].present?
classification model:
belongs_to :parent, class_name: "Classification", optional: true
has_many :children, class_name: "Classification", foreign_key: "parent_id", dependent: :destroy
has_many :class_fields
has_many :fields, through: :class_fields, source: :classification_field
accepts_nested_attributes_for :fields, allow_destroy: true
has_closure_tree
classification_field model:
has_many :class_fields
has_many :classifications, through: :class_fields
class_fields model:
belongs_to :classification
belongs_to :classification_field
Form where I am rendering dynamic form fields based on the classification fields details:
<%= form.fields_for :properties, OpenStruct.new(#sr.properties) do |builder| %>
<% #details.each do |field| %>
<%= render "srs/fields/#{field.field_type}", field: field, form: builder %>
<% end %>
<% end %>
Schema:
enable_extension "plpgsql"
create_table "class_fieldmembers", force: :cascade do |t|
t.integer "classification_id"
t.integer "classification_field_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "class_fields", force: :cascade do |t|
t.bigint "classification_id", null: false
t.bigint "classification_field_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["classification_field_id"], name: "index_class_fields_on_classification_field_id"
t.index ["classification_id"], name: "index_class_fields_on_classification_id"
end
create_table "classification_fields", force: :cascade do |t|
t.string "name"
t.string "field_type"
t.string "required"
t.string "classification_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "displayname"
end
create_table "classification_hierarchies", force: :cascade do |t|
t.integer "ancestor_id", null: false
t.integer "descendant_id", null: false
t.integer "generations", null: false
t.index ["ancestor_id", "descendant_id", "generations"], name: "classification_anc_desc_idx", unique: true
t.index ["descendant_id"], name: "classification_desc_idx"
end
create_table "classifications", force: :cascade do |t|
t.string "name"
t.string "displayname"
t.text "description"
t.boolean "inuse"
t.integer "sort_order"
t.integer "parent_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "srs", force: :cascade do |t|
t.string "summary"
t.text "description"
t.string "status"
t.string "priority"
t.bigint "user_id", null: false
t.bigint "classification_id", null: false
t.text "properties"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["classification_id"], name: "index_srs_on_classification_id"
t.index ["user_id"], name: "index_srs_on_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.boolean "admin"
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "class_fields", "classification_fields"
add_foreign_key "class_fields", "classifications"
add_foreign_key "srs", "classifications"
add_foreign_key "srs", "users"
Query results from console:
[1] pry(main)> ClassificationField.includes(:classifications).where(classification_id: [1, 6] )
ClassificationField Load (0.7ms) SELECT "classification_fields".* FROM "classification_fields" WHERE "classification_fields"."classification_id" IN ($1, $2) [["classification_id", "1"], ["classification_id", "6"]]
ClassificationField Load (0.6ms) SELECT "classification_fields".* FROM "classification_fields" WHERE "classification_fields"."classification_id" IN ($1, $2) /* loading for inspect */ LIMIT $3 [["classification_id", "1"], ["classification_id", "6"], ["LIMIT", 11]]
=> #<ClassificationField::ActiveRecord_Relation:0x4a10>
Any help is appreciated.
Try this. Convert query result to array of objects using .to_a
#details = ClassificationField.includes(:classifications).where(classification_id: [1, 6] ).to_a
I think I have it now.
#details = ClassificationField.joins(:class_fields).where(class_fields: {classification_id: [#parentids]}).to_a if params[:sub].present?

Associations for statistics

I'm new to RoR and I need some help with associations. I'm using rails 6.0.3.4 and ruby 2.7.0.
Users can create cases and cases belongs to a certain district. Districts belongs to a state. It has to be that way, because cases can't belongs to a state.
Now I want to show the number of cases for a certain diagnosis for each state. I have to use district, to get all the cases for a state. How should I build the where(...) condition?
<!-- State -->
<div class="card">
<div class="card-body">
<h5 class="card-title">State</h5>
<p><%= State.find(1).titel%> (<%= #diagnosis.cases.where(...).count %>)</p>
</div>
</div>
My Models
case.rb
class Case < ApplicationRecord
before_create :set_pseud
belongs_to :user
belongs_to :diagnosis
belongs_to :district
belongs_to :report, optional: true
end
district.rb
class District < ApplicationRecord
has_many :users
has_many :cases
has_many :reports
belongs_to :state
end
state.rb
class State < ApplicationRecord
has_many :districts
has_many :users
end
For better understanding my schema.rb:
ActiveRecord::Schema.define(version: 2021_02_11_140244) do
create_table "cases", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "gender"
t.date "birthdate"
t.string "place_of_residence"
t.string "diagnosis"
t.bigint "user_id"
t.datetime "confirmed_at"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6, null: false
t.bigint "diagnosis_id"
t.bigint "district_id"
t.bigint "report_id"
t.string "pseud"
t.index ["diagnosis_id"], name: "index_cases_on_diagnosis_id"
t.index ["district_id"], name: "index_cases_on_district_id"
t.index ["pseud"], name: "index_cases_on_pseud"
t.index ["report_id"], name: "index_cases_on_report_id"
t.index ["user_id"], name: "index_cases_on_user_id"
end
create_table "diagnoses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "illness"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "districts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name"
t.string "place"
t.integer "postal_code"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "state_id", null: false
t.index ["state_id"], name: "index_districts_on_state_id"
end
create_table "reports", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "district_id"
t.text "comment"
t.datetime "date"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["district_id"], name: "index_reports_on_district_id"
t.index ["user_id"], name: "index_reports_on_user_id"
end
create_table "states", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "titel"
t.string "abbr"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", 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.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "role"
t.string "first_name"
t.string "last_name"
t.bigint "district_id"
t.bigint "state_id"
t.index ["district_id"], name: "index_users_on_district_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["state_id"], name: "index_users_on_state_id"
end
end
You should add a further association into state:
has_many :cases, through: :districts
Rather than finding your state in the view, you should do that in the controller and pass it to the view in an instance variable:
#state = State.find(params[:id])
I've assumed you're using a show action here rather than manually coding the state ID for some reason.
You can then do something like this:
#state.cases.where(diagnoses: { id: #diagnosis.id }).count
Or if you prefer, you can skip the .id on #diagnosis:
#state.cases.where(diagnoses: { id: #diagnosis }).count

ActiveRecord query with multiple joins not recognizing the relations

I am trying to write an ActiveRecord Query that returns all students enrolled in a certain course with the following query:
def self.students_enrolled_in(course_id)
Student
.joins(:enrollments)
.joins(:sections)
.joins(:courses)
.where(sections: { course_id: course_id })
end
the result in the rails console is:
ActiveRecord::ConfigurationError: Can't join 'Student' to association named 'sections'; perhaps you misspelled it?
it seems that the association is made. what am I doing wrong? does the query actually mean that all the join() statements have to relate back to Student, or should ac trace out the relational links?
Professor show page:
<div class="col-md-8">
<h2 class="card-title"><%= #professor.name %></h2>
<% #courses_taught.each do |course| %>
<div class="card mb-4 card-header">
<img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
<h3 class="card-text"><%= course.title %></h3>
</div>
<div class="card-body">
<% course.sections.enrollments.students.each do |student| %>
<p><% student.name %></p>
<% end %>
</div>
<% end %>
</div>
models:
enrollment
class Enrollment < ApplicationRecord
belongs_to :section
belongs_to :student
end
Student:
class Student < ApplicationRecord
has_many :enrollments
end
Professor:
class Section < ApplicationRecord
has_many :enrollments
belongs_to :professor
belongs_to :course
validates_uniqueness_of :professor_id, scope: :course_id
scope :by_professor_id, ->(prof_id) { where('professor_id = ?', prof_id) }
end
Course:
class Course < ApplicationRecord
enum status: { planning: 0, offered: 1 }
scope :offered, -> { where(status: 1) }
scope :planning, -> { where(status: 0) }
belongs_to :department
has_many :sections
has_many :professors, through: :sections
validates :title, :number, :status, :description, presence: true
validates :description, length: { in: 10..500 }
validates :title, :number, uniqueness: { case_sensitive: false }
def self.search(term)
if term
where('title LIKE ?', "%#{term}%").order('title DESC')
else
order('title ASC')
end
end
def self.taught_by(professor_id)
Course
.joins(:sections)
.joins(:professors)
.where(sections: { professor_id: professor_id })
.select('distinct courses.*')
end
end
Schema:
ActiveRecord::Schema.define(version: 20171013201907) do
create_table "courses", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
t.integer "department_id"
t.index ["department_id"], name: "index_courses_on_department_id"
end
create_table "departments", force: :cascade do |t|
t.string "name"
t.text "description"
t.text "main_image"
t.text "thumb_image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "enrollments", force: :cascade do |t|
t.integer "section_id"
t.integer "student_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["section_id"], name: "index_enrollments_on_section_id"
t.index ["student_id"], name: "index_enrollments_on_student_id"
end
create_table "professors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
t.integer "department_id"
t.text "bio"
t.index ["department_id"], name: "index_professors_on_department_id"
end
create_table "sections", force: :cascade do |t|
t.integer "number"
t.integer "max_enrollment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "professor_id"
t.integer "course_id"
t.string "room"
t.index ["course_id"], name: "index_sections_on_course_id"
t.index ["professor_id", "course_id"], name: "index_sections_on_professor_id_and_course_id", unique: true
t.index ["professor_id"], name: "index_sections_on_professor_id"
end
create_table "students", force: :cascade do |t|
t.string "name"
t.decimal "gpa"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "name"
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.string "roles"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
Another way to do this is to add some more associations to your Student model:
class Student < ApplicationRecord
has_many :enrollments
has_many :sections, through: :enrollments
has_many :courses, through: :sections
scope :enrolled_in_course, -> (course) { joins(:sections).where(course_id: course.id)
end
You can then find all students enrolled in a course with:
Student.enrolled_in_course(course)
You're over-applying .joins. Try starting from the inside out. First, find the course:
Course.find_by(id: course_id)
Then, find all the sections associated with the course. No need to do a joins here:
Section.where(course: Course.find_by(id: course_id))
Now you do your join:
Student.joins(:enrollments).where(enrollments: {section: Section.where(course: Course.find_by(id: course_id))})
I think that ought to do the trick for you. But, untested. So, give it a go and see if it works.
P.S.: Try posting only the most relevant code. It's not so much fun to sort through a bunch of extraneous stuff.

Create categories in rails App

I would like to create categories in my app:
I got inspired by this question
But when #RailsGuy says: "Create some categories by categories controller and form (I don't think, I need to tell you that stuff, you are able to do it yourself)"
I felt lost...
How and where do I create my list of categories? dogs, cats, birds...
I saw that it could be done in the console but I will display a different pictures for each categories...
Here is my _form.html.slim
= simple_form_for #tuto do |f|
- if #tuto.errors.any?
#error_explanation
h2 = "#{pluralize(#tuto.errors.count, "error")} prohibited this tuto from being saved:"
ul
- #tuto.errors.full_messages.each do |message|
li = message
= f.hidden_field :user_id, value: current_user.id
= f.input :title
= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose Category"}
= f.input :content, as: :text, input_html: { rows: "15" }
= f.button :submit, "Save"
my models:
tuto_model.rb
class Tuto < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :category
validates :category, presence: true
end
categoty_model.rb
class Category < ActiveRecord::Base
has_many :tutos
end
schema.rb
ActiveRecord::Schema.define(version: 20160920133801) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "tutos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "content"
t.integer "user_id"
t.integer "category_id"
end
add_index "tutos", ["user_id"], name: "index_tutos_on_user_id"
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.string "first_name"
t.string "last_name"
t.boolean "admin"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "votes", force: :cascade do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "voter_id"
t.string "voter_type"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
end
thanks a lot for your help
Have the pictues stored in assets. And in the form/view use a conditional?:
<% if tuto.category == dog %>
<%= image_tag 'dog.jpg' %>
<% end %>
Something like that?
Steve.
The other post says to use the controller and form to create new categories. If you used scaffold to create the model Category, you'll have the new & create action in the controller and a new.html.erb and index.html.erb (or.slim if you're using that).
You can create your categories in there, then select one in your Tuto model.
Make sense?
Steve.

2 kind of user Rails with Devise

This is really a noob question but I've search a lot and didn't find nothing for my solution.
A have a application with 2 kind of user. Customer and Contractor. I try with devise to build both and that work I can sign in contractor and same with customer all is good in the db etc. But some things don't work.
First I can't add company name in the sign in form for the contractor(I already did that a lot but know don't work) I make the migration etc but can't show the in the sign in form. I have no error just no show.
Finally I try to make the customer build the project and have his Id with the
current_cutomer.projects.build(project_params)
and I get a error about argument etc.
Sry for my english
projects_controller.rb
class ProjectsController < ApplicationController
before_action :find_project, only: [:show, :quote, :edit, :update]
before_action :authenticate_cus!, except: [:index, :show]
def home
end
def index
#projects = Project.all.order("created_at DESC")
end
def show
end
def quote
end
def new
#project = current_customer.projects.create
end
def create
#project = current_customer.projects.create(project_params)
if #project.save
redirect_to #project
else
render 'new'
end
end
def edit
end
def update
if #project.update(project_params)
redirect_to #project
else
render 'edit'
end
end
private
def project_params
params.require(:project).permit(:name, :description, :date, :budget, :category_id, :location, :image)
end
def find_project
#project = Project.find(params[:id])
end
end
schema.rb
ActiveRecord::Schema.define(version: 20160305071807) do
create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
t.text "body"
t.string "resource_id", null: false
t.string "resource_type", null: false
t.string "author_type"
t.integer "author_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace"
add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
create_table "admin_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
end
add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true
add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "contractors", 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.string "company_name"
end
add_index "contractors", ["email"], name: "index_contractors_on_email", unique: true
add_index "contractors", ["reset_password_token"], name: "index_contractors_on_reset_password_token", unique: true
create_table "customers", 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
end
add_index "customers", ["email"], name: "index_customers_on_email", unique: true
add_index "customers", ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true
create_table "projects", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "budget"
t.date "date"
t.string "location"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "customer_id"
end
add_index "projects", ["category_id"], name: "index_projects_on_category_id"
add_index "projects", ["customer_id"], name: "index_projects_on_customer_id"
end
form for the contractor
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<h1>New Contractor</h1>
<div class="form-inputs">
<%= f.input :company_name, required: true, autofocus: true %>
<%= f.input :email, required: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "contractors/shared/links" %>
Finally I did two user model with devise and in the application controller I wrote
devise_group :user, contains: [:customer, :company]
Now I can use
#project = current_user.projects.build(project_params)
Everything Is ok and just need to give the permissions.

Resources