Rails 4 create model with nested attributes has_many - ruby-on-rails

I have a many to many relationship with DoctorProfile and Insurance. I'd like to create these associations off of a form from a client side app. I'm sending back an array of doctor_insurances_ids and trying to create the association in one line. Is it possible to send back an array of doctor_insurances ids? If so what's the proper way to name it for mass assignment in the params?
The error I'm getting with the following code is
ActiveRecord::UnknownAttributeError: unknown attribute 'doctor_insurances_ids' for DoctorProfile.
class DoctorProfile
has_many :doctor_insurances
accepts_nested_attributes_for :doctor_insurances # not sure if needed
class Insurance < ActiveRecord::Base
has_many :doctor_insurances
class DoctorInsurance < ActiveRecord::Base
# only fields are `doctor_profile_id` and `insurance_id`
belongs_to :doctor_profile
belongs_to :insurance
def create
params = {"first_name"=>"steve",
"last_name"=>"johanson",
"email"=>"steve#ymail.com",
"password_digest"=>"password",
"specialty_id"=>262,
"doctor_insurances_ids"=>["44", "47"]}
DoctorProfile.create(params)
end

You're not putting a doctor_insurance_id in your Doctor Profile so your DoctorProfile.create(params) line isn't going to work. You could do something like this:
def create
doctor = DoctorProfile.create(doctor_profile_params)
params["doctor_insurances_ids"].each do |x|
DoctorInsurance.create(doctor_profile_id: doctor.id, insurance_id: x)
end
end
def doctor_profile_params
params.require(:doctor_profile).permit(:first_name, :last_name, :email, :password_digest, :specialty_id)
end

Related

How can I create a model object with deeply nested associations in Rails using only my params whitelist method?

To replicate this question, my base model is project:
class Project < ApplicationRecord
has_many :tones, as: :analyzable
has_many :sentences
accepts_nested_attributes_for :tones, :sentences
end
And I have two other models:
class Sentence < ApplicationRecord
belongs_to :project
has_many :tones, as: :analyzable
accepts_nested_attributes_for :tones
end
class Tone < ApplicationRecord
belongs_to :analyzable, polymorphic: true
end
In my ProjectsController, I want to have a single create action that just takes in the whitelisted params and creates a project with all the appropriate associations. My project_params method looks like this:
private
def project_params
params.permit(:text, :title, :img, sentences_attributes: [:id, :text, tones_attributes: [:score, :tone_name]], tones_attributes: [:id, :score, :tone_name])
end
But if I try something like:
project = Project.create(project_params)
I will get an error like the one below:
| ActiveRecord::AssociationTypeMismatch (Sentence(#70119358770360) expected, got {"text"=>"extremely disappointed with the level of service with Virgin Media.", "tones"=>[{"score"=>0.64031, "tone_name"=>"Sadness"}, {"score"=>0.618451, "tone_name"=>"Confident
"}]} which is an instance of ActiveSupport::HashWithIndifferentAccess(#70119356172580)):
I ended up writing this for my create action, which gets the job done but feels quite clumsy:
project = Project.create({text: params[:text], title: params[:title],
img: params[:img]})
params[:sentences].each do |sent|
sentence = project.sentences.create({text: sent["text"]})
sent["tones"].each do |tone|
sentence.tones.create({score: tone["score"], tone_name: tone["tone_name"]})
end
end
params[:tones].each do |tone|
project.tones.create({score: tone["score"], tone_name: tone["tone_name"]})
end
Is there a better way to handle this kind of situation than what I have immediately above, where one is trying to create an object from nested params that has several levels of association?

Rails 5 "no implicit conversion of Symbol into Integer" for double nested has_many attributes

I keep getting the error no implicit conversion of Symbol into Integer when trying to create/update a Form model via a form. I've narrowed it down to something to do with actions_attributes in the form_params. trigger_attributes works fine if actions_attributes is removed. I suspect it has something to do with the enum field, double nested attributes, and/or the has_many relationship, but not sure.
Any ideas on what could be causing this error?
Running Rails 5.0.x and Ruby 2.3.x, with the relevant models and controller below.
class Form < ApplicationRecord
has_one :rule
accepts_nested_attributes_for :rule
end
class Rule < ApplicationRecord
has_one :trigger
has_many :actions
accepts_nested_attributes_for :trigger
accepts_nested_attributes_for :actions
end
class Trigger < ApplicationRecord
belongs_to :rule
enum name: [:example]
end
class Actions < ApplicationRecord
belongs_to :rule
enum name: [:example]
end
class FormsController < ApplicationController
...
private
def form_params
params.require(:form).permit(
:title,
:description,
rule_attributes: [
trigger_attributes: [:name],
actions_attributes: [:name]
]
)
end
end
I got this to work by changing actions_attributes: to actions: under def form_params, as well as making the relevant change to the form, changing fields_for :actions_attributes to fields_for :actions.
I'm often confused when to use _attributes and when not to. If anyone has information about when to use which, I would appreciate it if you could provide a link to the information in the comments. Thanks.

Rails: How to receive errors from nested models

I am trying to validate emails given from a CSV list of emails. So I have created the invite_list virtual attribute where when given a list of emails, it will loop and create a new record in the invited_only_emails model.
Now the thing is, this works fine, but how can I catch the validation error thrown by InvitedOnlyEmail while looping in Users model so I'll be able to use that error in the controller?
This is my main model:
class Users < ActiveRecord::Base
attr_accessor :invite_list
attr_accessible :invite_list
has_many :invited_only_emails
def invite_list=(list)
list.split(",").each do |address|
self.invited_only_emails.create! :email => address
end
end
def invite_list
self.invited_only_emails.map {|email| email.email}.join(',')
end
end
And this is the invited_only_emails model:
class InvitedOnlyEmail < ActiveRecord::Base
attr_accessible :email
belongs_to :users
validates_format_of :email, :with => /^([\w\.%\+\-]+)#([\w\-]+\.)+([\w]{2,})$/i
end
Thanks!
I think you could use validates_associated method:
class User < ActiveRecord::Base
validates_associated :invited_only_emails
# ...
end
http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_associated
Have you looked at validates_associated?
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_associated

Possible to specify what fields a belongs_to or has_many relationship object(s) return(s)?

A Contact has a User assigned to them:
class Contact < ActiveRecord::Base
...
belongs_to :user
...
end
The user model has a field I want to exclude any time a user object or objects are returned from db. One of the ways to make it work is to add a default scope:
class User < ActiveRecord::Base
...
has_many :contacts
...
default_scope select((column_names - ['encrypted_password']).map { |column_name| "`#{table_name}`.`#{column_name}`"})
end
So in console if I do:
User.first
The select statement and result set do not include 'encrypted_password'.
However, if I do:
c = Contact.includes(:user).first
c.user
they do. The default scope on the User model does not get applied in this case and the 'encrypted_password' field is shown.
So my question is why? And also, is there a clean way to specify what fields should be returned on related object(s)?
You should just be able to use the :select option on the belongs_to relationship. Something like this:
class Contact < ActiveRecord::Base
...
belongs_to :user, :select => [:id, :first_name, :last_name, :email]
...
end

Retrieving model attribute from table+column name

Let's say you have the following models:
class User < ActiveRecord::Base
has_many :comments, :as => :author
end
class Comment < ActiveRecord::Base
belongs_to :user
end
Let's say User has an attribute name, is there any way in Ruby/Rails to access it using the table name and column, similar to what you enter in a select or where query?
Something like:
Comment.includes(:author).first.send("users.name")
# or
Comment.first.send("comments.id")
Edit: What I'm trying to achieve is accessing a model object's attribute using a string. For simple cases I can just use object.send attribute_name but this does not work when accessing "nested" attributes such as Comment.author.name.
Basically I want to retrieve model attributes using the sql-like syntax used by ActiveRecord in the where() and select() methods, so for example:
c = Comment.first
c.select("users.name") # should return the same as c.author.name
Edit 2: Even more precisely, I want to solve the following problem:
obj = ANY_MODEL_OBJECT_HERE
# Extract the given columns from the object
columns = ["comments.id", "users.name"]
I don't really understand what you are trying to achieve. I see that you are using polymorphic associations, do you need to access comment.user.name while having has_many :comments, :as => :author in your User model?
For you polymorphic association, you should have
class Comment < ActiveRecord::Base
belongs_to :author, :polymorphic => true
end
And if you want to access comment.user.name, you can also have
class Comment < ActiveRecord::Base
belongs_to :author, :polymorphic => true
belongs_to :user
end
class User < ActiveRecord::Base
has_many :comments, :as => :author
has_many :comments
end
Please be more specific about your goal.
I think you're looking for a way to access the user from a comment.
Let #comment be the first comment:
#comment = Comment.first
To access the author, you just have to type #comment.user and If you need the name of that user you would do #comment.user.name. It's just OOP.
If you need the id of that comment, you would do #comment.id
Because user and id are just methods, you can call them like that:
comments.send('user').send('id')
Or, you can build your query anyway you like:
Comment.includes(:users).where("#{User::columns[1]} = ?", #some_name)
But it seems like you're not doing thinks really Rails Way. I guess you have your reasons.

Resources