Have many to many relationship but keep getting the same error: ActiveModel::UnknownAttributeError: unknown attribute 'employee_id' for EmployeeSkill. I've checked both my models and it seems like i have IDs for both so not sure what to do?
class Skill < ApplicationRecord
validates :skill_name, presence: true, length: { minimum: 3, maximum: 30}
validates_uniqueness_of :skill_name
has_many :employee_skills
has_many :employees, through: :employee_skills
end
...
class Employee < ApplicationRecord
before_save { self.email = email.downcase }
#validates :email, presence: true, length: { maximum: 30 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_many :employee_skills
has_many :skills, through: :employee_skills
end
...
class EmployeeSkill < ApplicationRecord
belongs_to :employee
belongs_to :skill
end
1) Check if in your migration files and database the corresponding id's are in place.
2) Restart the server (weird but sometimes is needed).
Your code looks good so I think is one of two cases I mention.
Regards,
Related
I have 4 database tables in Postgres. course, activity, lesson and task.
Currently when I do Course.find_by_id(params[:id])in my controller the models are setup to return all lessons in a course, but I'd like this nesting to extend to the activities in the lesson and include the one-to-one relationship of task to activity too.
class Course < ActiveRecord::Base
validates :title, uniqueness: { case_sensitive: false }, presence: true
has_many :lessons, -> { order(position: :asc) }, dependent: :restrict_with_error
belongs_to :subject
accepts_nested_attributes_for :lessons
end
class Lesson < ActiveRecord::Base
validates :title, uniqueness: { case_sensitive: false }, presence: true
validates :position, uniqueness: { scope: :course_id }, presence: true
belongs_to :course
has_many :activities, dependent: :restrict_with_error
acts_as_list column: :position, scope: :course
accepts_nested_attributes_for :course
end
class Activity < ActiveRecord::Base
validates :name, uniqueness: { case_sensitive: false }, presence: true
validates :task, presence: true
validates :term, presence: true
belongs_to :lesson
belongs_to :term
has_one :task, dependent: :destroy
accepts_nested_attributes_for :task, allow_destroy: true
attr_accessor :taskable_attributes
end
class Task < ActiveRecord::Base
validates :name, uniqueness: { case_sensitive: false }, presence: true
validates :taskable, presence: true
belongs_to :activity
belongs_to :taskable, polymorphic: true, dependent: :destroy
accepts_nested_attributes_for :taskable, allow_destroy: true
end
My JSON output currently looks like this:
{
"id": 1,
"title": "Algebra",
"description": "Do you know your y times table?",
"subject_id": 1,
"lessons": [
{
"id": 1,
"title": "Functions",
"description": "A mapping of inputs to outputs",
"position": 2,
"course_id": 1
}
]
}
But I would like it to contain more nested data
{
"id": 1,
"title": "Algebra",
"description": "Do you know your y times table?",
"subject_id": 1,
"lessons": [
{
"id": 1,
"title": "Functions",
"description": "A mapping of inputs to outputs",
"position": 2,
"course_id": 1,
"activities": [
{
"id": 1,
"title": "Activity 1"
"task":
{
"id": 1,
"name": "Task name here"
}
}
]
}
]
}
I've never used ruby or ruby on rails before, but being able to add this nesting to the frontend app until our rails developer returns would make my job a lot easier.
you can try in your responsible controller something like:
render json: #course.to_json( { include: [ lessons: { include: { :task } } ] } )
You can also exclude fields from any association with only, also you can include some methods with methods:{}
Cheers
I'm trying to ask the started_on value of parent in child_model.
I want to compare, if it has the same period
assumed, I have these two classes
class Parent < ActiveRecord::Base
has_many :children
end
And this class, Child_Class:
class Child < ActiveRecord::Base
belongs_to :parent
validates :name, presence: true, :length => { :minimum => 2, :maximum => 50 }
validates :finished_on, presence: true, date: {not_equal: :started_on}
validates :started_on, presence: true, date: {after: :parent.started_on}
end
What I need is the started_on value of parent
:parent.started_on returns me
undefined method 'started_on' for :project:Symbol
And I'm using this validators for my date
Validators
Thanks
class Child < ActiveRecord::Base
belongs_to :parent
validates :name, presence: true, :length => { :minimum => 2, :maximum => 50 }
validates :finished_on, presence: true, date: {not_equal: :started_on}
validate :validate_started_on
private
def validate_started_on
return if parent.nil?
return if started_on > parent.started_on
errors.add :started_on, :should_be_after_the_parent_started_on
end
end
You want to use it without a symbol.
i.e.
parent.started_on
As outlined in the guides
I am implementing something of a todo list with a user model and a List model with a date attribute.
On the user show page, I retrieve today's to do list.
How do I go about querying a user todo list for the previous and/or the next day.
All insights are welcome, thanks!
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_save { self.username = username.downcase }
has_many :to_do_lists, dependent: :destroy
has_many :tasks, dependent: :destroy
validates_presence_of :first_name, :last_name
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
VALID_USERNAME_REGEX = /\A[a-z_0-9]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :username, presence: true,
format: { with: VALID_USERNAME_REGEX },
uniqueness: { case_sensitive: false }
def name
[first_name, last_name].compact.join(' ')
end
end
and the list model
class ToDoList < ActiveRecord::Base
belongs_to :user
has_many :tasks, dependent: :destroy
validates_presence_of :user_id
validates :date, presence: true,
uniqueness: {scope: :user_id}
end
Rails adds many helpful methods to Time to make this type of query quite intuitive. Since you validate that a user has only one to do list for each day:
#next_day_list = #user.to_do_lists.find_by_date(Date.today.tomorrow)
#prev_day_list = #user.to_do_lists.find_by_date(Date.today.yesterday)
In our app's User model, we already have:
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
before_save { self.email = email.downcase }
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
Now, we need to add relationship associations to the model, namely:
has_many :roles, dependent: :destroy
has_many :agendas, through: :roles
Does it matter whether we include the latter go BEFORE or AFTER the former, in the model?
If so, what is the recommended / preferred / best way?
It doesn't matter, but the important thing is to be consistent. A usual best practice is to first do all you can to declare the class' structure, before you get in to any operational details. For example:
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
has_many :roles, dependent: :destroy
has_many :agendas, through: :roles
before_save :downcase_email
before_create :create_activation_digest
before_save { self.email = email.downcase }
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
end
Again, this is just one way to do things, but it's very common amongst Rails applications.
This simple validation test is failing:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
#user = User.new(name: "Example User",
email: "user#example.com",
character_attributes: {callsign: "example"},
password: "foobar",
password_confirmation: "foobar"
)
end
test "should be valid" do
assert #user.valid?, "#{#user.errors.messages}"
end
end
...with this message: character.sociable_id"=>["can't be blank"]
I don't understand why the user creation in UserTest is failing to make a valid User.
Each User has_one :character and each Character belongs_to a User.
The User model:
User.rb:
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
has_one :character, as: :sociable, dependent: :destroy
accepts_nested_attributes_for :character
has_secure_password
before_validation do
self.create_character unless character
end
before_save do
self.email.downcase!
end
before_create :create_activation_digest
validates :name, presence: true,
length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }, allow_blank: true
validates :character, presence: true
.
.
end
The Character model:
Character.rb:
class Character < ActiveRecord::Base
belongs_to :sociable, polymorphic: true
has_many :posts, dependent: :destroy
before_save do
self.callsign.downcase!
end
validates :sociable_id, presence: true
VALID_CALLSIGN_REGEX = /\A[a-z\d\-.\_]+\z/i
validates :callsign, presence: true,
length: { maximum: 20 },
format: { with: VALID_CALLSIGN_REGEX },
uniqueness: { case_sensitive: false }
end
It should be:-
test "should be valid" do
assert #user.valid? , "#{#user.errors.messages}"
end