NoMethodError (undefined method `scoped' for Class:Module): - ruby-on-rails

I have a has_many and has_many :through relationship that looks like this...
class Guestlist < ActiveRecord::Base
belongs_to :venues
has_many :patrons
has_many :users, :through => :patrons
attr_accessible :capacity, :end_time, :name, :start_time, :venue_id
end
class Patron < ActiveRecord::Base
belongs_to :guestlists
belongs_to :users
attr_accessible :guestlist_id, :user_id, :checked_in
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
has_many :patrons
has_many :guestlists, :through => :patrons
end
I am trying to access the users "through" guestlist object...
#guestlist = Guestlist.find(params[:id])
#guests = #guestlist.users.order('name ASC')
and the following error is thrown...
NoMethodError (undefined method `scoped' for Users:Module)
I've been searching all over for a solution but nothing works. Please help!

Looks something wrong with your associations in Model Patron. Singularize users and guestlists. Refer more here
class Patron < ActiveRecord::Base
belongs_to :guestlist
belongs_to :user
attr_accessible :guestlist_id, :user_id, :checked_in
end

Related

How to move a Rails one to many association to a one to one association?

User model has_many Companies and Company belongs_to a User.
I now want to change this to a user has_one Company and Company has_one/belongs_to association. I have changed the models to look like this:
user.rb
class User < ApplicationRecord
has_one :company #was has_many
accepts_nested_attributes_for :company
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
company.rb
class Company < ApplicationRecord
belongs_to :user
has_many :employees, inverse_of: :company
has_many :quotes, inverse_of: :company
accepts_nested_attributes_for :employees, reject_if: :all_blank, allow_destroy: true #, :reject_if => lambda { |e| e.first_name.blank? }
accepts_nested_attributes_for :quotes, allow_destroy: true
validates_presence_of :user, :co_name, :co_number, :postcode
validates :co_name, length: { minimum: 2, message: "minimum of 2 chars" }
validates :co_number, format: { with: /\A([1-9]\d{6,7}|\d{6,7}|(SC|NI|AC|FC|GE|GN|GS|IC|IP|LP|NA|NF|NL|NO|NP|NR|NZ|OC|R|RC|SA|SF|SI|SL|SO|SP|SR|SZ|ZC|)\d{6,8})\z/,
message: "must be valid company number" }
validates :postcode, format: { with: /\A(?:gir(?: *0aa)?|[a-pr-uwyz](?:[a-hk-y]?[0-9]+|[0-9][a-hjkstuw]|[a-hk-y][0-9][abehmnprv-y])(?: *[0-9][abd-hjlnp-uw-z]{2})?)\z/,
message: "must be valid postcode" }
enum industry: [ :financial_services, :architect, :business_consultancy ]
end
and have rake db:reset and have changed this line in my Companies#create method:
#company = current_user.companies.new(company_params)
to
#company = current_user.company.new(company_params)
But I'm getting a
undefined method `new' for nil:NilClass
and I can't see where I'm going wrong. current_user should be available? the has_one association is defined so I should be able to call company.new on it right? I don;t need a foreign_key added to User, or do I?
Can anyone help me with where I'm going wrong? Thank you.
undefined method `new' for nil:NilClass
For has_one, you should use build_association method, so
#company = current_user.company.new(company_params)
should be
#company = current_user.build_company(company_params)
Here is a list of all available methods for has_one association

Audited-activerecord gem not working properly for polymorphic associations

I want to make audits for nested associations like I have
User has_many addresses i.e either
HomeAddress or OfficeAddress
Now if I have just one table Address and I have used type and id for differentiating them.In this case if I use associated_audits for User then it will make just one audit record and whenever I update the record again ,its just replacing the previous audit with the last one.
Here is the models association:
class Patient < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable, :confirmable, request_keys: [:subdomain]
has_one :home_address,-> { where(addr_type: 'home') },class_name: 'Address'
has_one :office_address,-> { where(addr_type: 'office') }, class_name: 'Address'
has_associated_audits
accepts_nested_attributes_for :home_address, allow_destroy: true
accepts_nested_attributes_for :office_address, allow_destroy: true
end
class Address < ActiveRecord::Base
belongs_to :patient
audited
end
class Address < ActiveRecord::Base
belongs_to :patient
audited associated_with: :patient
end
class Patient < ActiveRecord::Base
has_many :addresses
has_associated_audits
end

Rails 4: has_many :through errors

Here is my join model:
class CompanyUser < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
My User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
ROLES = %w[admin requestor requestor_limited shipping_vendor].freeze
attr_accessor :temp_password
has_many :companies_users
...
end
If I run this in the console:
u = User.first
u.companies
This is the error I am getting:
NameError: uninitialized constant User::CompaniesUser
has_many through relationships should be like this:
In app/models/company.rb file,
has_many :company_users
has_many :users, :through => :company_users
In app/models/user.rb file,
has_many :company_users
has_many :companies, :through => :company_users
In app/models/company_user.rb file,
belongs_to :company
belongs_to :user
If you want to delete the dependent records in company_users table when deleting companies/users,
Add, , :dependent => :destroy at the end of has_many relations in Company and User model.
Hope this helps you..
Thanks.!!
it must be
has_many :company_users
"CompanyUser".tableize => "company_users"
The model shall be either:
class CompaniesUser < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
Or has_many declaration sheel be defined explicitly as:
class User < ActiveRecord::Base
has_many :company_users
end

What is <ActiveRecord::Associations::CollectionProxy []>?

I have these models:
class Item < ActiveRecord::Base
has_many :item_categoryships
has_many :categories, class_name: 'ItemCategoryship', foreign_key: 'category_id', :through => :item_categoryships
belongs_to :user
validates :title, presence: true
end
class Category < ActiveRecord::Base
has_many :item_categoryships
has_many :items, :through => :item_categoryships
belongs_to :user
validates :name, presence: true
end
class ItemCategoryship < ActiveRecord::Base
belongs_to :item
belongs_to :category
validates :item_id, presence: true
validates :category_id, presence: true
end
class User < ActiveRecord::Base
has_many :items
has_many :categories, class_name: 'Category'
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :async
end
But I found when I call item.categories will get a empty array !!! I have checked database, there is a record here.
When I test in the rails console, I didn't get any record back, just saw 'ActiveRecord::Associations::CollectionProxy []'.
What is this? I am using Rails 4.0.2.
Thanks you all.
ActiveRecord::Associations::CollectionProxy is ActiveRecord class for collection associations. Now, your code should work if you change line in Item class describing categories association to:
has_many :categories, through: :item_categoryships

Active_scaffold add to list associated table column

class Agency < ActiveRecord::Base
has_many :events
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :name, :email, :phone, :address, :city, :state, :zip,
:notes, :is_admin, :password, :password_confirmation, :remember_me
end
class Event < ActiveRecord::Base
belongs_to :agency
has_many :consumers
end
class Consumer < ActiveRecord::Base
belongs_to :event
end
In consumers_controller I am trying to include some field from agency
active_scaffold :consumer do |conf|
list.columns = [
:agency, :event
]
end
There are such associations Agency -> Event -> Consumer. And there is no association between agency and consumer, only through event.
but it causes an error.
How Can I include to list a any field form agency table?
According to the wiki, I think what you want is this:
active_scaffold :consumer do |conf|
conf.columns = [:agency, :event]
end
Also, make sure a consumer has an agency association or column.
Solution was a quite simple but most likely inefficient.
I have added a method to Consumer model:
class Consumer < ActiveRecord::Base
...
def agency_name
self.event.agency[:name]
end
end
Then I have added a virtual column to list:
list.columns = [
:agency_name, :event, ... ]
That's all.

Resources