I have 3 tables
User(id,email)
Org(id,name)
Role(id,user_id,org_id,role)
Ajax request send parameters
org[name] test
org[user][email] [email protected]
In my controller
def create
client_org = Org.new(client_org_params)
respond_to do |format|
@client_org.valid?
if !@client_org.errors.blank?
format.json { render json: @client_org.errors, status: :unprocessable_entity }
elsif Org.saveClientOrg(@client_org)
end
end
private
def client_org_params
params.require(:client_org).permit(:name,user_attributes: [:email])
end
In Org.model
def self.saveClientOrg(orgData)
org=ClientOrg.new(orgData.name)//-->okay I got name: test
orgData.user.email //error? undefined method `user' for #<Org>
end
Seem like this line code: orgData.user.email incorrect.
when I leave 2 input field blank and submit form ==> form validate says only name is blank and ignore email.Although in model user.rb I set validates :email, presence: true Anyone help me? Thanks in advance
EDIT
User.rb
has_many :roles
has_many :orgs, through: :roles
default_scope -> { order('email ASC') }
validates_format_of :email, :with => Devise.email_regexp
validates :email, presence: true
Org.rb
has_many :roles
has_many :users, through: :roles
validates :name, presence: true , uniqueness: { case_sensitive: false }
Role.rb
belongs_to :user
belongs_to :org
create
action of controller. – Leger