Displaying nested association in view in Rails 4 - ruby-on-rails

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 %>

Related

Rails cannot Create Record with Active Record Associations

My code works when I run the action like this,
def interest
#interest = Interest.new
#interest.user_id = current_user.id
#interest.idea_id = #idea.id
#interest.save
#ideas = Idea.take(10)
flash[:notice] = "A message has been sent to the poster."
render "ideas/forum"
end
But,why do I get an undefined method for 'interest' when I use this line in my Interest action?
#interest = current_user.ideas.interest.create(params[:interest])
Here's my Idea model
class Idea < ActiveRecord::Base
belongs_to :user
:title
:category
:content
:user_id
:createdDate
:updatedDate
Here's my User model (Devise)
class User < ActiveRecord::Base
has_many :ideas
has_many :interests
end
Here is the button_to tag
<%= button_to "I'm Interested", ideas_interest_path(:id => idea.id, :idea_id => idea.id, :user_id => idea.user_id) ,class: 'btn btn-primary' %>
And my route,
resources :ideas do
resources :interests
end
Interest Model
class Interest < ActiveRecord::Base
belongs_to :user
belongs_to :idea
has_many :users
:idea_id
:user_id
end
NoMethodError - undefined method `interest' for #<Idea::ActiveRecord_Associations_CollectionProxy:0x007f9e5189bab0>:
activerecord (4.2.0)
I think you messed up the association, I'd do:
class User < ActiveRecord::Base
has_many :interests
has_many :ideas, through: :interests
end
class Interest < ActiveRecord::Base
belongs_to :user
belongs_to :idea
# user_id, idea_id
end
class Idea < ActiveRecord::Base
has_many :interests
has_many :users, through: :interests
end
Then I guess the rest would work.

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

Show a name from associated class in ruby

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'?

NoMethodError? Counting Records

I have the following models:
class Label < ActiveRecord::Base
has_many :releases
end
class Release < ActiveRecord::Base
belongs_to :label
has_many :products
has_and_belongs_to_many :tracks
def self.releases_count
self.count(:all)
end
end
class Product < ActiveRecord::Base
belongs_to :release
has_many :releases_tracks, :through => :release, :source => :tracks
has_and_belongs_to_many :tracks
def self.products_count
self.count(:all)
end
end
On my label/index view i'm able to display a count of the Releases absolutely fine using:
<%= label.releases.releases_count %>
I'm trying to do the same for Products using:
<%= label.releases.products.products_count %>
But get a NoMethodError:
undefined method `products' for #<Label:0x10ff59690>
Any ideas?
I have lots of other aggregations I want to perform (Track Counts etc) so some guidance on where I'm going wrong would be really appreciated.
You need define your production/Label association
class Label < ActiveRecord::Base
has_many :releases
has_many :products, :through => :releases
end

rails has_many relationship (4 models) and how to access in the view

I have 4 models: transac, transac_data, item, dvd_details
class Transac < ActiveRecord::Base
has_many :transac_datas
has_many :items, :through => :transaction_datas
end
class TransactionData < ActiveRecord::Base
belongs_to :item
belongs_to :transaction
end
class Item < ActiveRecord::Base
has_many :transaction_datas
has_many :transacs, :through => :transaction_datas
end
class DvdDetails < ActiveRecord::Base
has_many :items
end
Now in the "transac" view, I need to access stuff from all these models like:
<td><%=h transac.status %></td>
<% transac.transaction_datas.each do |td| %>
<td><%=h td.item_type %></td>
<% end %>
<% transac.items.each do |item| %>
<td><%=h item.item_type %></td>
<% end %>
BUT I also need to access some info from the "DvdDetails" model which is the "furthest" away from transac.
I realized that doing something like this wouldn't really work:
class Transac < ActiveRecord::Base
has_many :transac_datas
has_many :items, :through => :transaction_datas
has_many :dvd_details, :through => :items, :through => :transaction_datas
end
and do this in the index of "transac" view
<%=h transac.dvd_details.name %>
What do I need to do to accomplish this?
Any help is appreciated!
Thank you!
Actually, with Ian White's nested_has_many_through plugin, you can daisy-chain has_many throughs the way you want. Just install the plugin like so:
script/plugin install git://github.com/ianwhite/nested_has_many_through.git
Then setup your model like this:
class Transac < ActiveRecord::Base
has_many :transaction_datas
has_many :items, :through => :transaction_datas
has_many :dvd_details, :through => :items
end
This should do what you need.
UPDATE: This question has come up a few times recently. I wrote an article, nesting your has_many :through relationships, to explain in detail. It even has an accompanying example application on GitHub to download and play around with.

Resources