Class with nested association to itself - ruby-on-rails-5

I have a class with a nested association to itself:
class Location < ActiveRecord::Base
has_many :location_gateways
has_many :gateways, through: :location_gateways, class_name: "Location", :dependent => :destroy
accepts_nested_attributes_for :gateways, reject_if: :all_blank, allow_destroy: true
belongs_to :location, optional: true
end
The connecting model:
class LocationGateway < ActiveRecord::Base
belongs_to :location, :class_name => "Location"
belongs_to :gateway, :class_name => "Location"
end
Now I would like to make a form that can create a Location and some gateways but I get an Gateway must exist error when I submit it (the call to Location.new)
I'm assuming this is because the model Gateway doesn't exist. How can I make rails understand that it should create another Locationand not a Gateway?

Found no way around this error. I had to make a new class Gateway that inherits from Location and add a type to Locations (STI).

Related

Using has_many through with nested namespaces

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.

Plural Name in Association in Rails

I have user model and a car model
I want to have a model which will hold the settings for each car and each user
so I do
class CarSettings < ActiveRecord::Base
belongs_to :user
belongs_to :car
end
for user:
has_many :car_settings
and for cars:
has_many :car_settings
has_many :users, :through => :car_settings
note the name CarSettings, this isn't a mistake, I want it to be settings and not setting
When I do
c=Car.first
c.users
I get
NameError: uninitialized constant Car::CarSetting
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/inheritance.rb:111:in `compute_type'
it is looking for a singular car_setting and not car_settings.
How can I fix this?
You can force the class name on the association using this option:
has_many :car_settings, :class_name => "CarSettings"

How can I reference the same model twice and create the association in a multiple select form?

I have two classes (Game and Report) and want to link them with an additional attribute (default = yes or no).
The game should then have default_reports and optional_reports.
The association is then updated by selecting the default and optional reports in a select (multiple) in the games create/edit form.
I have tried using has_many and through as well as polymorphic associations, but nothing seems to fit the use case, where the associated objects are fixed and you only want to manage associations.
class Game < ActiveRecord::Base
has_many :game_reports
has_many :reports, :through => :game_reports
end
class Report < ActiveRecord::Base
has_many :game_reports
has_many :games, :through => :game_reports
end
class GameReport < ActiveRecord::Base
belongs_to :game
belongs_to :report
end
Any help is appreciated!
Thanks
this is just the model. the view and form to create the records is an entirely different matter.
you can always add a conditions option to has_many. I'm assuming you're going to add default to game_reports so change your class to something like.
class Game < ActiveRecord::Base
has_many :game_reports
has_many :reports, :through => :game_reports
has_many :default_reports, through: :game_reports, source: :report, conditions: { game_reports: { default: true } }
end
Rails 4.2+, use a Polymorphic association with scope and specify the foreign_key and foreign_type options.
class GameReport
belongs_to :report, :polymorphic => true
end
class Game
has_many :game_reports, :as => :report, :dependent => :destroy
has_many :reports, -> { where attachable_type: "GameReport"},
class_name: GameReport, foreign_key: :game_report_id,
foreign_type: :game_report_type, dependent: :destroy
end
Other approachs:
Rails Polymorphic Association with multiple associations on the same model

RailsAdmin has_many direct creation

I have a simple has_many attachments situation:
class Project < ActiveRecord::Base
has_many :images, :class_name => 'ProjectImage', :dependent => :destroy
class ProjectImage < ActiveRecord::Base
has_attached_file :image
belongs_to :project
Is it possible (via Rails Admin) to directly add images when creating/editting project?
Now there are two ways (both suck!):
1) Create/Edit a ProjectImage instance and add it to the project (you have to search for it).
2) Add a new Project image which creates a modal and is afterwards same as 1)
The key is: nested attributes using accepts_nested_attributes_for.
eg:
has_many :images, :class_name => 'ProjectImage', :dependent => :destroy, :inverse_of => :project
accepts_nested_attributes_for :images, :allow_destroy => true

Two has_may association with Same model Rail 3

Hi im quite beginner in rails. i have a problem suggestion will be appreciated.
i have two model "user" and "asset"
an "asset" is created by a "user" and asset" can be assigned to a "user" schema is
Asset { id,name,creator_id,assigned_to_id,price,...}
User{ id,name,....}
now in Asset model class association are
class Asset < ActiveRecord::Base
{
#validation
belongs_to :creator ,:class_name=>'User'
belongs_to :assigned_to, :class_name=>'User' ,:foreign_key=>'assigned_to_id'
}
and User Model is
class User < ActiveRecord::Base
{
#any validation and other stuff
has_many :assets #did not specify either this association is for creator , or assigned_to user.how can is specify that??
}
now in Asset show view i can obtain creator name with
#asset.creator.name
but can't assigned_to name
#asset.assigned_to.name =>(error is )undefined method `first_name' for nil:NilClas
and
#asset.assigned_to_id.name=>(error is) undefined method `first_name' for 1:Fixnum
any suggestion how can i make double association with same model
ok solution was in my last comment.
Multiple relation with same model
class Asset < ActiveRecord::Base
belongs_to :creator ,:class_name=>'User'
belongs_to :assigned_to, :class_name=>'User'
end
user.rb
class User < ActiveRecord::Base
has_many :created_assets, :foreign_key => 'creator_id', :class_name => 'Asset'
has_many :assigned_assets , :foreign_key => 'assigned_to_id', :class_name => 'Asset'
end