Show a name from associated class in ruby - ruby-on-rails

I have Aulas and Students Through Grades,
in grades I want to display the name of the student and the name of the Aula.
<% #grades.each do |grade| %>
<%= grade.student.name %>
<%= grade.aula.name %>
<% end %>
If I leave only the student I get it perfect, But when I want to get Aula name, I get:
undefined method `aula' for #<#<Class:0x30a37e8>:0x2fffeb0>
Here is my code
class Aula < ActiveRecord::Base
attr_accessible :name
has_many :grades
has_many :students, :through => :grades
end
class Student < ActiveRecord::Base
attr_accessible :name
has_many :grades
has_many :aulas, :through => :grades
end
class Grade < ActiveRecord::Base
attr_accessible :grammar, :oral, :participation, :writing
belongs_to :aula
belongs_to :student
end
I figure that the problem is that if grade.aula.name is nil, I get this error. If the data is there, it works perfectly.
How can I do an action like, if grade.aula.name.nill? grade.aula.name = 'write the name here'?

Related

Displaying nested association in view in Rails 4

I have 3 models as below:
class Kick < ActiveRecord::Base
has_many :offs
has_many :retailers, :through => :off
end
class Retailer < ActiveRecord::Base
has_many :offs
has_many :kicks, :through => :off
end
class Off < ActiveRecord::Base
belongs_to :kicks
belongs_to :retailers
end
And I'm trying to display the name of the retailer in my 'show Kick view' as below:
<% #kick.off.each do|off| %>
<%= off.name %>
<%= off.retailers.name %>
<% end %>
Off.name displays fine but I cannot seem to index the retailer's name from this view. What am I missing?
Error:
undefined method `name' for nil:NilClass
class Kick < ActiveRecord::Base
has_many :offs
has_many :retailers, :through => :offs
end
class Retailer < ActiveRecord::Base
has_many :offs
has_many :kicks, :through => :offs
end
class Off < ActiveRecord::Base
belongs_to :kick
belongs_to :retailer
end
also make sure you properly indexed the models in db
class Kick < ActiveRecord::Base
has_many :offs
has_many :retailers, :through => :offs
end
class Retailer < ActiveRecord::Base
has_many :offs
has_many :kicks, :through => :offs
end
class Off < ActiveRecord::Base
belongs_to :kick
belongs_to :retailer
end
#kick = Kick.includes(:retailers => :offs).where('kicks.id' => 1).select('retailers.name, kicks.*')
In the view it should be kick.offs not kick.off
<% #kick.offs.each do|off| %>
<%= off.name %>
<%= off.retailers.name %>
<% end %>

uninitialized constant error when joining 3 tables in view

I have 3 tables. pin, genre and genres_pins.
genres_pins joins the pin and genre tables together with a many to many. Here's my setup:
Pin Model
class Pin < ActiveRecord::Base
belongs_to :user
belongs_to :type
has_many :replies
has_many :genres_pins
has_many :genres, :through => :genres_pins
end
Genre Model
class Genre < ActiveRecord::Base
has_many :genres_pins
has_many :pins, :through => :genres_pins
end
GenresPins Model
class GenresPins < ActiveRecord::Base
belongs_to :pin
belongs_to :genre
end
View
<% pin.genres_pins.each do |g| %>
<%= g.title %>
<% end %>
I get the following error:
uninitialized constant Pin::GenresPin
Any idea what's going on here? I'm new to Rails, so may be missing something stupidly obvious.
Help appreciated.
Many thanks,
Michael.
class GenrePin < ActiveRecord::Base
belongs_to :pin
belongs_to :genre
end
The name of the class should be changed

Searching one model by another

My application consists of a drink model
class Drink < ActiveRecord::Base
attr_accessible :name
has_many :recipe_steps, :dependent => :destroy
has_many :ingredients, through: :recipe_steps
end
An ingredient model
class Ingredient < ActiveRecord::Base
attr_accessible :name
has_many :recipe_steps
end
how would I go about having it so when a user searches an ingredient that it returns all of the drinks with that ingredient?
Additional information: I'm currently using sunspot/solr for my searching.
First, in your Ingredient model you'd need this line:
has_many :drinks, through: :recipe_steps
To define the has_many, through: relationship. Make sure that RecipeStep has these lines, too:
belongs_to :ingredient
belongs_to :drink
Then you can do something like in the DrinksController:
def search
term = params[:search]
ingredient = Ingredient.where(:name => term)
#drinks = Ingredient.find(ingredient).drinks
end
And your form should look something like this:
<%= form_for #drink, :url => { :action => "search" } do |f| %>
<%= f.text_field :search %>
<% end %>
I don't know all your names for everything but this should get you going.
Following should work fine:
class Ingredient < ActiveRecord::Base
...
has_many :recipe_steps
has_many :drinks, through: :recipe_steps
end

accepts_nested_attributes_for - belongs_to, has_many, fields_for

Is there any possible way to use nested_attributes_for in the way show below?
Basically I want to create a person, one or more cars and add details to each car. This is just a mock up, not a very realistic example. I get snagged when trying to build the details for the car as it hasn't been created yet.
Models:
class Person < ActiveRecord::Base
has_many :cars
accepts_nested_attributes_for :car
end
class Car < ActiveRecord::Base
belongs_to :person
has_many :details
accepts_nested_attributes_for :details
end
class Detail < ActiveRecord::Base
belongs_to :car
end
Form:
form_for #person do |f|
#fields
f.fields_for :car do |car|
#fields
car.fields_for :details |detail|
=detail.text_field :content
end
end
end
Have a look at that http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast

collection_select with has_many :through relationship table data

I have such terrible models:
class ParentalRelation < ActiveRecord::Base
belongs_to :parent
belongs_to :student
belongs_to :counselor
belongs_to :parental_relation_type
end
class ParentalRelationType < ActiveRecord::Base
has_many :parental_relations
end
class Parent < ActiveRecord::Base
has_many :parental_relations
has_many :students, :through => :parental_relations
has_many :counselors, :through=> :parental_relations
has_many :parental_relation_types, :through=> :parental_relations
belongs_to :user, :dependent=> :destroy
belongs_to :occupation_type
accepts_nested_attributes_for :user
end
Parental relation types are like father, mother, etc. The reasoning is that a parental relation between one counselor, one parent and one student is unique and counselors should not see the relations that belong other counselors.
In controllers/parent_controller/edit action I have:
#parental_relation= ParentalRelation.find_by_counselor_id_and_student_id_and_parent_id(x, y, z)
In views/parent/_form.html.erb I have:
<%= form_for #parent do |f| %>
inside that form I need a collection_select for ParentalRelationType.all and select the parent's parental_relation_type_id for that particular parental relation, but I can't find the syntax to do it.
I tried adding
<%= collection_select(#parental_relation, :parental_relation_type_id, ParentalRelationType.all, :id, :name) %>
underneath the form_for, but the relation type id is 2, and default 1 is selected instead.
Added this to parents/_form
<%= fields_for #counselor_student_parent do |csp| %>
<%= f.label :parental_relation_type_id %>
<%= collection_select(:student_counselor_parent, :parental_relation_type_id, ParentalRelationType.all, :id, :name) %>
<% end %>
And this to parents_controller/new
def new
#counselor= Counselor.find(params[:counselor_id])
#student= Student.find(params[:student_id])
#parent= #student.parents.build
#parent_user= #parent.build_user
#counselor_student_parent= #counselor.student_counselor_parents.build
end

Resources