I have two models:
- Project.
- Item.
A project has many items, and an item belong to a project.
class Project < ActiveRecord::Base
attr_accessor :main_color, :secondary_color, :tertiary_color
has_many :items
accepts_nested_attributes_for :items
validates :main_color, :presence => true
validates :secondary_color, :presence => true
validates :tertiary_color, :presence => true
end
class Item < ActiveRecord::Base
belongs_to :project
validates :title, :presence => true,
:length => {:maximum => 300}
validates_presence_of :project
end
Every project has main, secondary, and tertiary colors.
I want to store them in my database (PostgreSQL) as an array of strings (and I already setup a migration for that), but I have to display them separately in a form using color input fields, as well as validate them.
I've used virtual attributes to achieve that so the colors aren't saved separately, but I still want to save them properly as an array, how should I do that?
I also have another issue happening in my Item model related to the colors.
Each Item validates the presence of a project. I have a single page where a user can add multiple items to a project, and since
the project requires these three colors and those are messing up, I keep getting validation errors : main_color can't be blank
, secondary_color can't be blank
, tertiary_color can't be blank
.
This is my create item method in my ItemsController
def create
@project = Project.find(params[:project_id])
return if !@project
items = @project.items.build( \
project_params.to_h['items_attributes'].hash_with_index_to_a)
@project.items.append(items)
if @project.save
redirect_to "/projects/#{@project.id}/items"
else
render 'items/new'
end
end
And this is the related strong params:
def project_params
params.require(:project).permit(:items_attributes =>
[
:id,
:title
]
)
end
How should I go about implementing this?