Seems that made some mistake in the model
class LineItemAddons < ActiveRecord::Base
belongs_to :line_item
has_one :addon_type_value
attr_accessible :quantity, :received, :snapshot_price, :status, :line_item_id, :addon_type_value_id
class LineItem < ActiveRecord::Base
belongs_to :design
belongs_to :cart
belongs_to :designer_order
belongs_to :return
belongs_to :return_designer_order
has_many :line_item_addons
class AddonTypeValue < ActiveRecord::Base
belongs_to :design
belongs_to :addon_type
belongs_to :line_item_addon
attr_accessible :description, :name, :position, :price, :addon_type_id, :design_id, :quantity
Getting unintialized constant for line_item_addons
I think it should be
class AddonTypeValue < ActiveRecord::Base
belongs_to :design
belongs_to :addon_type
belongs_to :line_item_addons
...
You don't need to describe association belongs_to with has_one.
http://guides.rubyonrails.org/association_basics.html#the-has-one-association
Try to remove belongs_to :line_item_addon from AddonTypeValue model
I made a mistake while generating the model
the class name should LineItemAddon not LineItemAddons
Sorry Guys
Related
I have a nested model like so:
class Games::Player < ActiveRecord::Base
attr_accessible :user_id
belongs_to :user
has_many :games_extras_achievements_players, :class_name => 'Games::Extras::AchievementsPlayer'
has_many :games_extras_achievements, :class_name => 'Games::Extras::Achievement',:through=>:games_extras_achievements_players
validates :user_id,uniqueness: true
end
class Games::Extras::Achievement < ActiveRecord::Base
has_many :games_extras_achievements_players, :class_name => 'Games::Extras::AchievementsPlayer'
has_many :games_players, through: :games_extras_achievements_players, class_name: 'Games::Player'
end
class Games::Extras::AchievementsPlayer < ActiveRecord::Base
attr_accessible :games_extras_achievement_id, :games_player_id
belongs_to :games_extras_achievement, :class_name => 'Games::Extras::Achievement'
belongs_to :games_player, :class_name => 'Games::Player'
end
Objects on the join class work as expected.
However trying to get player -> achievement or vice versa gives an error:
> p.games_extras_achievements
Games::Extras::Achievement Load (0.3ms) SELECT "games_extras_achievements".* FROM "games_extras_achievements" INNER JOIN "games_extras_achievements_players" ON "games_extras_achievements"."id" = "games_extras_achievements_players"."games_extras_achievement_id" WHERE "games_extras_achievements_players"."player_id" = 1
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column games_extras_achievements_players.player_id does not exist
LINE 1: ...ents_players"."games_extras_achievement_id" WHERE "games_ext...
If I change the migration to use player_id like it is trying to find, I get an error stating that games_player_id does not exist
I seem to have fixed it.
I needed to use the non-namespaced column names and add a foreign key constraint.
class Games::Player < ActiveRecord::Base
attr_accessible :user_id
belongs_to :user
has_many :games_extras_achievements_players, :class_name => 'Games::Extras::AchievementsPlayer'
has_many :games_extras_achievements, :class_name => 'Games::Extras::Achievement',:through=>:games_extras_achievements_players
validates :user_id,uniqueness: true
end
class Games::Extras::Achievement < ActiveRecord::Base
has_many :games_extras_achievements_players, :class_name => 'Games::Extras::AchievementsPlayer'
has_many :games_players, through: :games_extras_achievements_players, class_name: 'Games::Player'
end
class Games::Extras::AchievementsPlayer < ActiveRecord::Base
attr_accessible :achievement_id, :player_id
belongs_to :games_extras_achievement, class_name:'Games::Extras::Achievement',foreign_key: :achievement_id
belongs_to :games_player, class_name: 'Games::Player',foreign_key: :player_id
end
Hopefully this will save someone some aggravation.
Has anyone found a way to write an rspec example for an ActiveRecord polymorphic association?
I'm used to using Thoughtbot's shoulda matches, but I think polymorphic's are beyond it's scope?
Just for clarity my models would have a similar pattern to this:
class Person < ActiveRecord::Base
attr_accessible :name
has_one :address, as: :location_data_source
accepts_nested_attributes_for :address
end
class Company < ActiveRecord::Base
attr_accessible :name
has_one :address, as: :location_data_source
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
attr_accessible :street, :city
belongs_to :location_data_source, polymorphic: true
end
I have an event, a vendor, and a venue model.
An event can have multiple vendors but one venue.
A venue can have multiple vendors and multiple events.
A vendor can have multiple venues and multiple events.
I have an events table, a venues table, a vendor table, an events_vendor table and a events_venue table.
How do I have to setup my models?
class Event < ActiveRecord::Base
attr_accessible :name, :budget, :client, :date, :description, :attendees,
:assets_attributes, :tag_list
belongs_to :user
has_many :assets, :dependent => :destroy
has_many :vendors
has_one :venue
accepts_nested_attributes_for :assets, :allow_destroy => true
acts_as_taggable
end
class EventsVendors < ActiveRecord::Base
attr_accessible :event_id, :vendor_id
end
class EventsVenues < ActiveRecord::Base
attr_accessible :event_id, :venue_id
end
class Vendor < ActiveRecord::Base
attr_accessible :city, :contact, :country, :description, :email, :employees,
:latitude, :longitude, :minimum, :name, :state, :street, :tel, :type
belongs_to :event
end
class Venue < ActiveRecord::Base
attr_accessible :capacity, :city, :contact, :country, :email, :exclusiveVendors, :fee,
:latitude, :longitude, :name, :state, :street, :tel, :union
belongs_to :event
has_many :vendors
end
Does my events_venues model belong to both events and venues? Do I have to specify the :through relationship?
Any help would be appreciated. thanks!
Yes, the join tables should look something like this.
EventsVenues
belongs_to :event
belongs_to :venue
Event
has_many :venues, through: :events_venues
has_many :events_venues
Venues
has_many :events, through: :events_venues
has_many :events_venues
I have a has_many through relationship between Course and User.
class Course < ActiveRecord::Base
belongs_to :user
has_many :enrollments, :dependent => :delete_all
has_many :users, :through => :enrollments
attr_accessible :description, :duration, :name, :prerequisites, :short_name, :start_date, :user_id
accepts_nested_attributes_for :users, :allow_destroy => true
attr_accessible :users_attributes
and User:
class User < ActiveRecord::Base
has_many :subjects, :class_name => "Course" # to get this call user.subjects
has_many :enrollments, :dependent => :delete_all
has_many :courses, :through => :enrollments
and Enrollment:
class Enrollment < ActiveRecord::Base
belongs_to :course
belongs_to :user
attr_accessible :course_id, :user_id
end
Now I'm trying to set user_ids from inside Course, using a nested form. It keeps giving me that Mass Assignment warning, and nothing is saved. I read I was supposed to add attr_accessible user_id but it still doesn't work.
Even if I do something like this from the rails console:
#c.update_attributes({:user_ids => [7,8]})
with #c being the course
Any help would be greatly appreciated,
Thank you.
It's user_ids, not user_id.
You need to add user_ids to your attr_accessible.
I using rails3 and trying to build some complex associations.
I have Product, Version and Property models.
class Version < ActiveRecord::Base
belongs_to :product
has_many :specs
has_many :properties, :through => :specs
end
class Product < ActiveRecord::Base
has_many :versions
has_many :specs
has_many :properties, :through => :specs
end
class Property < ActiveRecord::Base
end
class Spec < ActiveRecord::Base
belongs_to :product
belongs_to :spec
belongs_to :version
end
It works perfect, but i want to use product and version as polymorphic relations, so table specs will have only spec_id and some_other_id, instead of spec_id, product_id, version_id.
I can't figure out where i should put :as and where :polymorphic => true. Can you help me?
How about:
class Version < ActiveRecord::Base
belongs_to :product
has_many :specs, :as => :speccable
has_many :properties, :through => :specs
end
class Product < ActiveRecord::Base
has_many :versions
has_many :specs, :as => :speccable
has_many :properties, :through => :specs
end
class Property < ActiveRecord::Base
end
class Spec < ActiveRecord::Base
belongs_to :speccable, :polymorphic => true
belongs_to :spec
end
#table: specs(id,spec_id,speccable_type,speccable_id)