rails3 view has_many :through attribute - ruby-on-rails-3

Three models linked via has_many :through
class Bozza < ActiveRecord::Base
has_many :accessoryvolumes, :dependent => :destroy
has_many :accessories, through: :accessoryvolumes
belongs_to :lavorazione
class Accessory < ActiveRecord::Base
has_many :accessoryvolumes
has_many :bozzas, through: :accessoryvolumes
class Accessoryvolume < ActiveRecord::Base
belongs_to :accessory
belongs_to :bozza
In the view for bozza, the attributes for bozza are accessible
<% #bozza.accessoryvolumes.each do |accessoryvolume| %>
<%= accessoryvolume.numero %>
<%= accessoryvolume.bozza_id %>
<%= accessoryvolume.bozza.lavorazione.name %>
<%= accessoryvolume.accessory_id %>
<%= accessoryvolume.accessory.name %>
save for the last item. Any attribute for the relationship to accessory generates and
undefined method `name' for nil:NilClass
evan though accessory_id has a value. How is the related attribute in one instance being picked up and not the other?

The issue is with the plural handling of "accessory". it was a nasty suspicion for many hours....
This issue arises rather frequently. Pony up and come up with a name that cannot be interpreted mistakenly by rails or someone, somehow, somewhere. Avoid nouns that pluralize irregularly. Moreso when dealing with foreign languages.
Now
class Accessorio < ActiveRecord::Base
has_and_belongs_to_many :lavoraziones
has_many :accessoryvolumes
has_many :bozzas, through: :accessoryvolumes
and
<%= accessoryvolume.accessorio.nome %>
runs as expected

Related

Grouping posts by user in has_many through relation collection with Rails 5

I'm trying to display Users (devise) in a collection (has_many through).
<%= #collection.posts.count %> designs by
<% #collection.posts.each do |post| %>
<%= link_to image_tag(post.designer.avatar.url.to_s, class: "avatar-small"), post.designer %>
<% end %>
but it displaying duplicates as in
I need to group by designer.id. So I added #collection_designers line but I could not manage to group by designer id.🤷‍♂️
class CollectionsController < ApplicationController
def show
#collection = Collection.find(params[:id])
#I need to group for designer.id I guess but I could not manage it
#collection_designers = Collection.find(params[:id]).groups(designers.id)
end
...
Relations:
models/collection.rb
class Collection < ApplicationRecord
belongs_to :designer
has_many :collectivizations
has_many :posts, through: :collectivizations
end
models/collectivization.rb
class Collectivization < ApplicationRecord
belongs_to :post
belongs_to :collection
end
models/post.rb
class Post < ApplicationRecord
belongs_to :category
belongs_to :designer
has_many :collectivizations
has_many :collections, through: :collectivizations
You need to find uniq designers ids at first
designer_ids = #collection.posts.distinct.pluck(:designer_id)
And after it find designers by these ids
#designers = Designer.where(id: designer_ids)

Find_or_initializes_by for a complex model

Newbie Rails user here so sorry if I'm totally overlooking something; I'm just looking for direction on what resources I should use to solve this problem.
I have a Rails app running that uses virtual attributes to define a complicated has_many :through relationship. What I'm looking to do is to use the id of an existing entry rather than creating a new one when a user submits a form. I have attempted to use find_or_initializes_by and find_or_create to no avail. Depending on how I set it up, it either changes one of the diagnosis columns on the join table successfully (but leaves the other diagnosis column blank, causing a rollback), or it doesn't work at all.
Background for my use case: a Case can have many diagnoses (Diagnosis), some of which are primary, and others are "differential" diagnoses. Cases and multiple diagnoses are related in a join table.
My current setup:
case.rb
def new
#case = Case.new
#case_and_diagnoses = #case.case_and_diagnoses.build
#primary_diagnoses = #case_and_diagnoses.build_primary_diagnosis
#differential_diagnoses = #case_and_diagnoses.build_differential_diagnosis
end
The relevant section of the case form:
<%= f.fields_for :case_and_diagnoses do |x| %>
<%= x.fields_for :primary_diagnosis do |diagnosis| %>
<%= diagnosis.label :name, "Diagnosis" %>
<%= diagnosis.text_field :name %>
<% end %>
<%= x.fields_for :differential_diagnosis do |differential| %>
<%= differential.label :name, 'Differential diagnosis' %>
<%= differential.text_field :name %>
<% end %>
<% end %>
What's the best way to tackle this problem? I don't need an explicit code block to use, but I'm looking for a strategy so I can maybe find my own way. Thank you very much!
Update:
The models:
class Case < ApplicationRecord
before_save :get_diagnoses
belongs_to :submitter, class_name: 'User', foreign_key: 'submitter_id'
belongs_to :resident, class_name: 'User', foreign_key: 'resident_id'
belongs_to :faculty, class_name: 'User', foreign_key: 'faculty_id'
has_one :service
has_many :case_and_diagnoses
has_many :primary_diagnoses, through: :case_and_diagnoses
has_many :differential_diagnoses, through: :case_and_diagnoses
accepts_nested_attributes_for :case_and_diagnoses
end
class Diagnosis < ApplicationRecord
has_many :case_and_diagnoses
has_many :cases, through: :case_and_diagnoses
end
class CaseAndDiagnosis < ApplicationRecord
belongs_to :case
belongs_to :primary_diagnosis, class_name: 'Diagnosis', foreign_key: :primary_diagnosis_id, optional: true
belongs_to :differential_diagnosis, class_name: 'Diagnosis', foreign_key: :differential_diagnosis_id, optional: true
accepts_nested_attributes_for :primary_diagnosis, :differential_diagnosis
end

How to use "accepts_nested_attributes_for" in rails 3?

I'm following the docs and toying with simple_form, formtastic, and nested_form gems with no success. This very simple example yields empty for for me:
resort.rb
class Resort < ActiveRecord::Base
attr_accessible :address, :description, :latitude, :longitude, :name, :phone, :second_name,
:resort_type_id, :infrastructure_attributes
validates_presence_of :name, :address, :phone, :description
has_many :infrastructures
belongs_to :resort_type
accepts_nested_attributes_for :infrastructures
end
infrastructure.rb
class Infrastructure < ActiveRecord::Base
attr_accessible :name, :description, :infrastructure_type_id, :resort_id
belongs_to :resort
belongs_to :infrastructure_type
end
form view
= form_for #resort do |f|
= f.fields_for :infrastructures do |i|
= i.text_field :name
Seems I missed something obvious but can't figure out what exactly is wrong with the code.
If I may ask, in your controller action did you build the appropriate code for your infrastructure. Something like
3.times { #resort = #resort.infrastrucures.build }
To my knowledge you need something like this for your form before it can build the proper nested form content

Rails, get object association when using build (object still not in database), the DRY

Im building a report system which uses a sort of meta question model. Questions are previusly saved in the database, and then depending of the type of report some questions are taken from the database.
Wanting to keep things DRY, i'm trying to figure out a way to pass the information of the Variable model to my report_header with no avail.
In the new action i have:
reportBody = #report_head.report_bodies.build(:variable_id => a.id)
#report_head.variables #modified, thx.
all i need is to pass the attributes from the Variable to report_head in a DRY way.
If you need to know my models:
class Variable < ActiveRecord::Base
attr_accessible :id,:break_point, :description, :name, :time_frequency, :v_type
has_many :report_bodies
has_many :report_heads, :through => :report_bodies
end
class ReportHead < ActiveRecord::Base
attr_accessible :email, :name , :report_bodies_attributes, :report_bodies, :variables_attributes
has_many :report_bodies
has_many :variables, :through => :report_bodies
accepts_nested_attributes_for :report_bodies
end
class ReportBody < ActiveRecord::Base
attr_accessible :report_head_id, :variable_value, :variable_id, :variables_attributes, :report_heads
belongs_to :report_head
belongs_to :variable
end
Update
I updated the model as suggested, and modified the way to call the variables. However im still confused about how to use it in the view, if i do something like:
<%= f.fields_for :variables do |variable| %>
<%= variable.text_field :name, :value => :name, :class => 'text_field' %>
<% end %>
it prints a string instead of the actual name.
You have define wrong name association, your association of ReportBody should be:
belongs_to :report_head
belongs_to :variable
This is not correct:
#report_head.report_bodies.build(:variable_id => a.id,:report_head_id =>#report_head.id)
chang it to:
#report_head.variables.build(:variable_id => a.id)
it's better, you don't have to set report_head_id. And this is wrong:
#report_head.report_bodies.variables
If you want to get all variables belong to #report_head, you just need using:
#report_head.variables

Does accepts_nested_attributes_for work with belongs_to?

I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship?
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
In a view:
= form_for #user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
Nested attributes appear to work fine for a belongs_to association as of Rails 4. It might have been changed in an earlier version of Rails, but I tested in 4.0.4 and it definitely works as expected.
The doc epochwolf cited states in the first line "Nested attributes allow you to save attributes on associated records through the parent." (my emphasis).
You might be interested in this other SO question which is along the same lines as this one. It describes two possible solutions: 1) moving the accepts_nested_attributes to the other side of the relationship (in this case, Organization), or 2) using the build method to build the Organization in the User before rendering the form.
I also found a gist that describes a potential solution for using accepts_nested_attributes with a belongs_to relationship if you're willing to deal with a little extra code. This uses the build method as well.
For belongs_to association in Rails 3.2, nested model needs the following two steps:
(1) Add new attr_accessible to your child-model (User model).
accepts_nested_attributes_for :organization
attr_accessible :organization_attributes
(2) Add #user.build_organization to your child-controller (User controller) in order to create column organization.
def new
#user = User.new
#user.build_organization
end
For Ruby on Rails 5.2.1
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
Just got to your controller, suppose to be "users_controller.rb":
Class UsersController < ApplicationController
def new
#user = User.new
#user.build_organization
end
end
And the view just as Nick did:
= form_for #user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
At end we see that #user3551164 have already solved, but now (Ruby on Rails 5.2.1) we don't need the attr_accessible :organization_attributes