adding index in rails - activerecord

I
I have added has_many and belongs_to after running rake db:migrate. I added these associations and again rake db:migrate. No index was generated. Can you please tell me why is that?
Below are my associations
class Developer < ActiveRecord::Base
has_many :evaluations
has_many :final_scores
belongs_to :supervisor
end
class Evaluation < ActiveRecord::Base
belongs_to :developer
belongs_to :supervisor
end
class FinalScore < ActiveRecord::Base
belongs_to :developer
end
class Supervisor < ActiveRecord::Base
has_many :developers
has_many :evaluations :through => :developers
end

Have a look at the official rails guide on this subject: http://edgeguides.rubyonrails.org/association_basics.html#the-has-many-association
There you can see that you need to change the generated migration file and add the field by yourself.

This blog helps to understand better. You may missed out something in migration file.

Related

Rails 4 query a reference via n:n relation

following structure
Production
belongs_to :offer
has_many :production_positions
has_many :products, through: :production_positions
Product
has_many :production_positions
has_many :productions, through: :production_positions
ProductionPosition
belongs_to :production
belongs_to :product
Now i want to get all offers which contains the product_id 1. How did i have to query the database?
To get the productions is quite easy:
Production.includes(:products).where("products.id" => 1)
but how do i get the offers which are referenced?
Offer.includes(:production).includes(:products).where("products.id" => 1)
the line above produces the following error:
Association named 'products' was not found on Offer
Add has_many :products, through: :production in your Offer class and try with the following:
Offer.select('offers.*').joins(:products).where('products.id = ?', 1)
I assumed you had has_one :production in your Offer class. You could add a scope to perform the query:
class Offer < ActiveRecord::Base
has_one :production
has_many :products, through: :production
scope :with_product_id, ->(id) do
select('offers.*').
joins(:products).
where('products.id = ?', id)
end
end
Example (Offers with Product 1):
Offer.with_product_id(1)

Active Record has_many through more than one model

Is it possible to access objects more than one model away?
For example let's say I have
class Contact <ActiveRecord:Base
has_many :interactions
end
class Interaction <ActiveRecord:Base
belongs_to :contact
belongs_to :course_presentation
end
class CoursePresentation <ActiveRecord:Base
has_many: interactions
belongs_to :course
end
class Course <ActiveRecord:Base
has_many :course_presentations
end
Right now I know I could write a through relationship via contacts to course presentations and then get all the course related to all the course presentations or I could do
contact.interactions.map{ |i| i.course_presentation.course }
I would like to be able to pull courses related to a contact directly so ... e.g.
contact.courses
Is this possible?
Yes, I believe so. Just add the following:
class Contact < ActiveRecord::Base
has_many :interactions
has_many :course_presentations, through: :interactions
has_many :courses, through: :course_presentations
end

NameError: uninitialized constant - Un-allowed syntax in ActiveRecord?

I have two resources that I'm trying to join - Package and Listing through a join table SubmittedPackage. I'm using Ruby 1.9.3-p125 and Rails 3.2.1 with PostgreSQL 9.1.3. The models look as follows.
class Package < ActiveRecord::Base
has_many :submitted_packages
has_many :listings, :through => :submitted_packages
class Listing < ActiveRecord::Base
has_many :submitted_packages
has_many :packages, :through => :submitted_packages
class SubmittedPackages < ActiveRecord::Base
belongs_to :package
belongs_to :listing
In Rails Console I keep getting NameError: uninitialized constant Listing::SubmittedPackage
If I replace the SubmittedPackage resource with Drum it will work (this of course includes having the appropriate table created and so forth).
Is :submitted_packages in conflict with something in Rails or ActiveRecord?
Any ideas why this is breaking?
Thanks in advance!
UPDATE: As a work-around I explicitly defined the :class_name for the has many relationship in the Listing and Package model. This has at least gotten things working, however, it's still is unclear to me why it was necessary to begin with. What Rails or Ruby naming convention was being broken by :submitted_packages ?
class Package < ActiveRecord::Base
has_many :submitted_packages, :class_name => 'SubmittedPackages'
has_many :listings, :through => :submitted_packages
class Listing < ActiveRecord::Base
has_many :submitted_packages, :class_name => 'SubmittedPackages'
has_many :packages, :through => :submitted_packages
class SubmittedPackages < ActiveRecord::Base
belongs_to :package
belongs_to :listing
If SubmittedPackage is the JoinTable, I guess it should have many listings, but I'm seeing just many packages and many submitted_packages.
HTH

rails belongs_to through via association

I'm on rails 3.0 and trying to figure out what would be the proper way to setup a belong_to :through relationship (which) I know is not possible. Here's an example:
class ParentCompany < ActiveRecord::Base
has_many :subsidiaries
has_many :employees, :through => :subsidiaries
end
class Subsidiary < ActiveRecord::Base
belongs_to :parent_company
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :subsidiary
belongs_to :parent_company, :through :subsidiary # <-- I know this is invalid
end
I know I can solve it by doing:
class Employee < ActiveRecord::Base
def parent_company
subsidiary.parent_company
end
end
However, I'd like to know if I can do the above via associations.
You can use delegate to accomplish this without using an association
class Employee < ActiveRecord::Base
belongs_to :subsidiary
delegate :parent_company, to: :subsidiary
end

ActiveRecord: Has many through (twice)

There are Things in Places which I'm looking to find. One Thing could be in many different Places, and many Things can be in one Place.
class Thing < ActiveRecord::Base
has_and_belongs_to_many :places
end
class Place < ActiveRecord::Base
has_and_belongs_to_many :things
end
I want to record the Finds of my Users so that I know where they found what.
class Find < ActiveRecord::Base
belongs_to :user
belongs_to :places_thing # Is this depluralization correct?
end
class User < ActiveRecord::Base
has_many :finds
# Now, how can I link in the Things the user has found? Like this?
has_many :found_things_in_places, :class_name => :places_things, :through => :finds
has_many :things, :through => :thought_things_in_places
end
Does this seem right? is it efficient? Thanks.
I think you were on the right track, the big change I'd make is that rather than having a join table (places_things) you should make it a proper model. I decided to call this an existence.
The data only exists in one place, so it's properly normalized. These relationships are clear and will be easy to manage. I think it's efficient.
class Place < ActiveRecord::Base
has_many :existences
has_many :things, :through => :existences
end
class Thing < ActiveRecord::Base
has_many :existences
has_many :places, :through => :existences
end
class Existence < ActiveRecord::Base
belongs_to :place
belongs_to :thing
end
class Find < ActiveRecord::Base
belongs_to :user
belongs_to :existence
end
class User < ActiveRecord::Base
has_many :finds
has_many :existences, :through => :finds
has_many :things, :through => :existences
end
You'll need rails 3.1 to do the nested has many through's like we did in User.
BTW the correct association declaration should be: belongs_to :places_things

Resources