Getting undefined method qouted_table_name on associations - ruby-on-rails

I'm having problems fixing association that is working on a rails 2 application. I'm this error on my local machine:
undefined method `quoted_table_name' for Enrollment:Module
I can't seem to figure out where the error is.
Model:
class Enrollment < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :enrollment
accepts_nested_attributes_for :enrollment
end
Controller:
class UsersController < ApplicationController
def new
#user = User.new
#user.build_enrollment
end
end
UPDATE:
Fixed the problem because the name of the model is the same as the name of the rails application.

Did you run rake db:migrate to make sure the tables exist
Make the following change since the nested attributes clause is misplaced
class Enrollment < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :enrollment
# This line belongs here right?
accepts_nested_attributes_for :enrollment
end

Related

Renaming associations in Rails 5

class Reminder < ApplicationRecord
has_and_belongs_to_many :offices, class_name: :failed_offices
end
class Office < ApplicationRecord
has_and_belongs_to_many :reminders
end
class OfficesReminder < ApplicationRecord
belongs_to :office
belongs_to :reminder
end
If I do this in rails console I am getting an error
p=Reminder.last
p.failed_offices
undefined method `failed_offices' for Reminder:0x00007ff87014df78
I want to rename the association for offices. So I tried failed_offices as class_name in Reminder class but it didn't do the trick. Please suggest how it is possible.

undefined method build_list (Rails 5)

user.build_book does not seem to work. I get an undefined method build_book error. However,
a= user.book.build
a.save
does work. Why would that be?
rails g migration add_user_reference_to_book user:references
class Book < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :books
end
I would like to try explain your question,
parent.build_child(attributes) this for has_one relationship (parent has_one child)
if you using has_many then you should use parent.children.build(attributes)
if user has_one book then #user.build_book will work
if user has_many books then user #user.books.build

Namespaced key lookup on Rails 5 association with ActiveRecord

I have the following models:
class Supervision::ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.table_name_prefix
"supervision_"
end
end
---------------
class Supervision::Activity < Supervision::ApplicationRecord
has_one :supervision_missed_visit, class_name: Supervision::MissedVisit
(...)
end
---------------
class Supervision::MissedVisit < Supervision::ApplicationRecord
belongs_to :supervision_activity, class_name: Supervision::Activity
(...)
end
And I also have this model, which isn't namespaced:
class Activity < ApplicationRecord
(...)
end
Whenever I try to reach the Supervision::MissedVisit through its has_one relationship, as in
#supervision_activity.supervision_missed_visit
I get the following error:
ERROR: column supervision_missed_visits.activity_id does not exist
How do I make it so that Rails understands that I'm actually looking for supervision_missed_visits.supervision_activity_id?
You could specify the foreign key:
has_one :supervision_missed_visit, class_name: Supervision::MissedVisit, foreign_key: 'supervision_activity_id'
Another way to do it would be to create an instance method in Supervision::Activity:
def missed_visit
Supervision::MissedVisit.where(supervision_activity_id: id).take
end

Rails: building object from has_many associated object

I think it might be a syntax problem.
I have a Event.rb model:
class Event < ActiveRecord::Base
belongs_to :members
has_one :brazusc
end
A Member.rb:
class Member < ActiveRecord::Base
has_many :events
end
And a Brazusc.rb model:
class Brazusc < ActiveRecord::Base
belongs_to :event
end
What I am trying to do: member.events.build_brazusc, but I get:
NoMethodError: undefined method `build_brazusc' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f99517d4a78>
How would I build an object from the has_many association?
I am also trying to retrieve the associated model from a has_many/has_one association, like this: member.events.brazusc, since brazusc has an event_id, but I get:
NoMethodError: undefined method `leads' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f99517d4a78>
How can I retrieve the object (brazusc) that is associated with event?
Any help will be much appreciated.
This would do the trick:
new_brazusc = member.events.find_by(name: "Brazusc")
new_brazusc.build_brazusc.save

RAILS: How to make new collection.build in a callback?

How to create a new record in after_save using other model?
I tried this line which resulted "undefined method `journals' for nil:NilClass"
e.g.
resources :users do
resource :profile
resources :journals
end
class User < ActiveRecord::Base
has_one :profile
has_many :journals
end
class Profile < ActiveRecord::Base
belongs_to :user
after_save :create_new_journal_if_none
private
def create_new_journal_if_none
if user.journals.empty? ????
user.journals.build() ????
end
end
end
class Journals < ActiveRecord::Base
belong_to :user
end
Nested models are going to be saved as well once parent saves, so it's easy to use before_create block and build a nested resource here.
class Profile < ActiveRecord::Base
belongs_to :user
before_create do
user.journals.build unless user.journals.any?
end
end
This line of code will create a profile and a journal assigned with the User
User.find(1).create_profile(name :test)

Resources