My system is going to be large, so I've separated the models in namespaces. However, there is a model which I cannot setup the relation...
(My models are in portuguese, and the plurals are ok)
class Sistema::Instituicao < ActiveRecord::Base
has_many :agencias
has_many :dependencias, through: :agencias
#(...)
end
class Sistema::Agencia < ActiveRecord::Base
belongs_to :instituicao
has_many :dependencias
#(...)
end
class Sistema::Dependencia < ActiveRecord::Base
belongs_to :agencia
belongs_to :instituicao, through: :agencia
#(...)
end
But I get an error in Dependencia, as follows:
ArgumentError: Unknown key: through
What am I not seeing?
Thanks!
if you look at the api for belongs_to you will see that there's no :through option. the :through option is only available for has_one and has_many associations
Related
I have a has_many through, through relationship that needs to be eagerly loaded. Is this possible in Rails 3.2? I've tried several ways to include the association yet calling categories on the facebook_user object always makes another query to Category. Here's a simplified version of my activerecord associations.
class FacebookUser < ActiveRecord::Base
belongs_to :user, touch: true
has_many :user_categories, through: :user
has_many :categories, through: :user_categories
end
class User < ActiveRecord::Base
has_many :user_categories
has_many :categories, through: :user_categories
has_one :facebook_user
end
class UserCategory < ActiveRecord::Base
belongs_to :user
belongs_to :category
end
#single query
FacebookUser.includes( user: :categories).joins(user: :categories).each do |f|
## N+1 query on f.categories
f.categories.first
end
#multi-part query
facebook_user_ids = FacebookUser.where(user_id: [1,2,3]).joins(user: :categories).pluck('facebook_users.id')
FacebookUser.where( id: facebook_user_ids ).includes( user: :categories)
With
FacebookUser.includes( user: :categories)
you have eager loaded the user and his categories instead of the categories on facebook_user. So in order to avoid n+1 query you call then
f.user.categories
So why u just don't eager facebook_user's categories?
FacebookUser.includes(:categories)
Suppose I have a result from a query:
allCourses = Course.all
Then I also have another set:
myCourses = current_user.courses.all
How can I get a set of items that are in allCourses and NOT in myCourses?
Here are the models:
class Student < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :courses, through: :student_enrollments
end
class Course < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :students, through: :student_enrollments
end
class StudentEnrollment < ActiveRecord::Base
belongs_to :student
belongs_to :course
end
I can always write raw SQL script to achieve the result, but I prefer to find a Rails way to do it.
Thanks
Assume your fk is user_id.
ohterCourses = Course.where('user_id != ?', current_user.id)
I have three models Agreement, Service and Price.
class Agreement < ActiveRecord::Base
has_many :prices, as: :priceable
end
class Service < ActiveRecord::Base
has_many :prices, as: :priceable
end
class Price < ActiveRecord::Base
attr_accessible :price, :price_currency, :priceable_id, :priceable_type
belongs_to :priceable, polymorphic: true
end
But I have two price types in service customer_price and agency_price. Agreement hasn't price type. I want to model something like below. How it is possible?
class Agreement < ActiveRecord::Base
has_many :prices, as: :priceable
end
class Service < ActiveRecord::Base
has_many :customer_prices, as: :priceable # I think I should write something here
has_many :agency_prices, as: :priceable # I think I should write something here
end
class Price < ActiveRecord::Base
attr_accessible :price, :price_currency, :priceable_id, :priceable_type
belongs_to :priceable, polymorphic: true
end
What is best approach? May be I should make two price model like AgreementPrice and ServicePrice. Best Regards.
Making two models wouldn't help you actually.
I think you only need to set a specific foreign_key to your relations.
has_many :customer_price, as: :priceable, :foreign_key => customer_price_id
has_many :agency_price, as: :priceable, :foreign_key => agency_price_id
Those two fields must be added in your schema.
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
Getting Following Error
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection in ProjectController#create
Cannot modify association 'ProjectMaster#tag_masters' because the source reflection class 'TagMaster' is associated to 'ProjectTag' via :has_many.
Following are my models.
class ProjectTag < ActiveRecord::Base
has_many :tag_masters
has_many :project_masters
end
class TagMaster < ActiveRecord::Base
has_many :project_tags
has_many :project_masters, :through => :project_tags
end
class ProjectMaster < ActiveRecord::Base
has_many :project_tags
has_many :tag_masters, :through => :project_tags
# Some more code and associations here..
end
I am new to rails and tried to solve it but I don't think i can change my associations.
I am using rails 3.0.10
Please help me out here.
Thanks
I think my associations were wrong.
class ProjectTag < ActiveRecord::Base
has_many :tag_masters
has_many :project_masters
end
instead of has_many; I had to use belongs_to.