uninitialized constant error when joining 3 tables in view - ruby-on-rails

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

Related

Filtering records by ownership

Sorry about the title, couldn't come up with a better one to describe what I'm trying to do.
In a previous question, some users suggested I could simplify the models. I didn't get anymore comments but I think I'm doing it 'the right way' because I need to store additional attributes in the join tables.
Anyway my models are setup like this.
class Student < ActiveRecord::Base
has_many :student_notes
has_many :notes, :through => :student_notes
has_many :relationships
has_many :users, :through => :relationships
end
class Note < ActiveRecord::Base
has_many :student_notes
has_many :students, :through => :student_notes
end
class StudentNote < ActiveRecord::Base
belongs_to :student
belongs_to :note
end
class User < ActiveRecord::Base
has_many :relationships
has_many :students, :through => :relationships
has_many :notes, :through => :students
end
class Relationship < ActiveRecord::Base
belongs_to :student
belongs_to :user
end
Now, in my note show view I have this:
...
<% #note.students.each do |student| %>
<tr>
<td><%= link_to student.full_name, student %></td>
</tr>
<% end %>
...
This works, except I would like to show only the students that belong to current_user.
Any tips on how can I achieve it? I guess a helper method in the model would be the way to go, but I'm totally lost.
Thank you!
On Student:
scope :belonging_to, -> (u) { joins(:users).where users: { id: u.id } }
Then:
#note.students.belonging_to(current_user)...

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

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

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

Rails Polymorphic with Lookup Table

What's the best way to do this? I want to be able to give Bands and Artists genres through polymorphism. I can do it with habtm and has_many :through but I'm trying to figure out if it's possible through polymorphism.
GenreList would be a lookup table with a list of different genres (e.g. Punk, Pop, Metal). I've reviewed Ryan Bate's screencast for Polymorphic Assoiciations but I'm still stuck. Specifically, I'm not sure how to create the polymorphic table Genre which would be fed canned genres from the GenreList model (the lookup table).
Is the following correct?
rails generate model Genre genre_list_id:integer genreable_id:integer genreable_type:string
class Artist < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Band < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Genre < ActiveRecord::Base
belongs_to :genreable, :polymorphic => true
end
class GenreList < ActiveRecord::Base
end
I think your implementation is a little bit weird. The way I would do it is to create a model Genre (which it will hold all the available genres Punk, Rock, Metal etc). Then I would do all this that you've already done but without the GenreList model:
rails g model Genre genreable_id:integer genreable_type:string genre_name:string
class Artist < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Band < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Genre < ActiveRecord::Base
belongs_to :genreable, :polymorphic => true
end
Then I would do make some nested resources in my routes as:
resources :artists do
resources :genres
end
resources :bands do
resources :genres
end
and then edit my controller to handle this nested relation. With this approach say if i want to see all the genres of the first artist I would visit:
/artists/1/genres
same holds for bands. I hope that I understood your problem. Let me know if I helped!
Ok, after 6.5 hrs, I managed to figure this out. I used the inherited_resources gem to help with the controllers. To recap, I wanted to be able to add Genres to Artists and Bands through a polymorphic relationship, i.e. Genres would be a lookup table, and Genreings would be a polymorphic model that contains genres for Artists and Bands. Below is the code that worked for me:
# Generate some scaffolding
rails generate scaffold Artist name:string
rails generate scaffold Band name:string
rails generate scaffold Genre name:string
rails generate scaffold Genreing genre_id:integer genreable_id:integer genreable_type:string
# Models
class Artist < ActiveRecord::Base
has_many :genreings, :as => :genreable
has_many :genres, :through => :genreings
end
class Band < ActiveRecord::Base
has_many :genreings, :as => :genreable
has_many :genres, :through => :genreings
end
class Genre < ActiveRecord::Base
attr_accessible :name
has_many :genreings
end
class Genreing < ActiveRecord::Base
attr_accessible :genre, :genre_id, :genreable, :genreable_type, :genreable_id
belongs_to :genre
belongs_to :genreable, :polymorphic => true
end
# Controller
class GenreingsController < InheritedResources::Base
belongs_to :genreable, :polymorphic => true
end
# Artist Form View
= simple_form_for(#artist) do |f|
.inputs
= f.input :name
= f.association :genres, :as => :check_boxes
.actions
= f.button :submit
# Band Form View
... (Similar to Artist)
It is correct. One thing that seems to be missing is the has_many relationship from GenreList to Genre
class GenreList < ActiveRecord::Base
has_many :genres
end

Resources