I have the following
class Service < ActiveRecord::Base
has_many :service_testimonials, class_name: 'Service::Testimonial', dependent: :destroy
has_many :testimonials, through: :service_testimonials
end
class Service::Testimonial < ActiveRecord::Base
belongs_to :service
belongs_to :testimonial
end
class Testimonial < ActiveRecord::Base
has_many :service_testimonials, class_name: 'Service::Testimonial', dependent: :destroy
has_many :services, through: :service_testimonials
end
But if I do Service.first.testimonials the sql is SELECT "service_testimonials".* FROM "service_testimonials" INNER JOIN "service_testimonials" "service_testimonials_testimonials_join" ON "service_testimonials"."id" = "service_testimonials_testimonials_join"."testimonial_id" WHERE "service_testimonials_testimonials_join"."service_id" = $1 [["service_id", 1]]
So it returns a collection of Service::Testimonial not of Testimonial, adding class_name to the through doesn't help. Can I get this to work? Or do I simply need to rename my model?
You need to make changes in Service::Testimonial class:
class Service::Testimonial < ActiveRecord::Base
belongs_to :service, class_name: '::Service'
belongs_to :testimonial, class_name: '::Testimonial'
end
Those class_names are obligatory, because Rails assumes, you are looking for a class in the same namespace. So, for instance declaration belongs_to :service will be interpreted into belongs_to :service, class_name: 'Service::Service', which obviously is not your intention.
Related
Given these 4 Rails models:
class Apple < ActiveRecord::Base
has_one: ?
end
class Banana < ActiveRecord::Base
has_one: ?
end
class FruitMapping < ActiveRecord::Base
belongs_to :fruit, polymorphic: true
has_one :cart
end
class Cart < ActiveRecord::Base
end
How can I connect the has_one of the Apple/Banana to Cart, so that when I write apple.cart I will get the relevant Cart (through the mappings table)?
class Apple < ActiveRecord::Base
has_one :fruit_mapping, as: :fruit
end
class Cart < ActiveRecord::Base
has_many :fruit_mappigns
has_many :apples, through: :fruit_mappings, source: :fruit, source_type: 'Apple'
has_many :bananas, through: :fruit_mappings, source: :fruit, source_type: 'Banana'
end
Using the source and source_type options, you can define the polymorphic relationships. If using source and source_type are depricated in the Rails version you're using you can try
has_many :apples, through: :fruit_mappings, class_name: 'Apple', foreign_key: :fruit_id
I have a Products & Parts model which would each have multiple uploads, which are also polymorphic. Is it possible for me to have a single ItemUpload model to handle the association between the Products/Parts and Uploads, or do they need to be separate? I'd try myself just to see, but don't want to cause any potential headaches down the line! Note that I'm aware I need to do the source: and source_type: stuff to clean up the polymorphic association with has_many, but would like to clarify this point first before proceeding. Current models:
class Product < ApplicationRecord
has_many :uploads, as: :uploadable, dependent: :destroy
end
class Part < ApplicationRecord
has_many :uploads, as: :uploadable, dependent: :destroy
end
class Upload < ApplicationRecord
belongs_to :uploadable, polymorphic: true
end
What I would ideally like:
Class ItemUpload < ApplicationRecord
belongs_to :product, optional: true
belongs_to :part, optional: true
belongs_to :upload
end
Is that ok or would I need a separate ProductUpload and PartUpload model?
I would have thought your associations would look more like:
class Product < ApplicationRecord
has_many :item_uploads, as: :itemable, dependent: :destroy
has_many :uploads, through: :item_uploads
end
class Part < ApplicationRecord
has_many :item_uploads, as: :itemable, dependent: :destroy
has_many :uploads, through: :item_uploads
end
class Upload < ApplicationRecord
has_many :item_uploads
has_many :products, through: :item_uploads, source: :itemable, source_type: 'Product'
has_many :parts, through: :item_uploads, source: :itemable, source_type: 'Part'
end
Class ItemUpload < ApplicationRecord
belongs_to :itemable, polymorphic: true
belongs_to :upload
end
That should allow you to do:
product.uploads
part.uploads
upload.products
upload.parts
BTW, in reference to the link you provided:
Upload ≈ User
ItemUpload ≈ Membership
Product, Part ≈ Project, Group
The above follows the pattern in the linked article.
I have 4 Models and i am not sure what is the correct way to write my relationships/associations.
class User < ActiveRecord::Base
has_many :boards
has_many :lists
has_many :cards
end
class Board < ActiveRecord::Base
belongs_to :user
has_many :lists
end
class List < ActiveRecord::Base
belongs_to :user
belongs_to :board
has_many :cards
end
class Card < ActiveRecord::Base
belongs_to :user
belongs_to :list
end
If you want to be more explicit about your relationships, feel free to do the following (preferred in most every case):
class User < ActiveRecord::Base
has_many :boards, inverse_of: :user, dependent: :destroy
has_many :lists, inverse_of: :user, dependent: :destroy
has_many :cards, inverse_of: user, dependent: :destroy
end
class Board < ActiveRecord::Base
belongs_to :user, inverse_of: :boards
has_many :lists, inverse_of: :board
end
class List < ActiveRecord::Base
belongs_to :user, inverse_of: :lists
belongs_to :board, inverse_of :lists
has_many :cards, inverse_of :list
end
class Card < ActiveRecord::Base
belongs_to :user, inverse_of: :cards
belongs_to :list, inverse_of :cards
end
Finally, make sure any of your models that are dependent on another (e.g. Board belongs_to User) have the appropriate foreign key in their table. So, for example, Board will need to have a user_id foreign key to correctly create the association.
You can create a migration for any of those entities if you haven't already like so:
rails generate migration AddUserRefToBoards user:references
I'm trying to create a join table between 2 models that has 3 columns. The models are called User and Dare. The join table is called DaresUsers. And what I want to include in the join table is an author_id, accepter_id, dare_id.
Each dare will only have one author but will have many accepters because more than one user can accept the same dare. Should I use a has_many through relationship and if so what would I declare in my models? My confusion here is because the User model is referenced by the join table in two respects: the author_id and accepter_id.
Thanks for any help!
Try this:
class Dare < ActiveRecord::Base
belongs_to :author, class_name: 'User'
has_many :dare_user_relations, dependent: :destroy
has_many :accepters, through: :dare_user_relations
end
class DareUserRelation < ActiveRecord::Base
belongs_to :accepter, class_name: 'User'
belongs_to :dare
end
class User < ActiveRecord::Base
has_one :dare, foreign_key: 'author_id', dependent: :destroy
has_many :dare_user_relations, dependent: :destroy
has_many :dares, through: :dare_user_relations, foreign_key: 'accepter_id'
end
or w/o a model:
class Dare < ActiveRecord::Base
belongs_to :author, class_name: 'User'
has_and_belongs_to_many :accepters, class_name: 'User', association_foreign_key: 'accepter_id'
end
class User < ActiveRecord::Base
has_one :dare, foreign_key: 'author_id', dependent: :destroy
has_and_belongs_to_many :dares, foreign_key: 'accepter_id'
end
How can I add ownership in many to many relationships?
For example like this models.
class User < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :files, through: :editabilities
end
class File < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :users, through: :editabilities
end
class Editabilities < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
And I want to add a one-to-many relationship to User-and-Files.
At first I thought it is best to add owner boolean column to Editabilities, but I have no idea how to handle it.
Secondly I thought if I make a new junction model Ownerships, then I can handle it same way as Editabilities. But I've got a uninitialized constant User::Ownership when I tried it with code like this.
class User < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :ownerships, dependent: :destroy
has_many :files, through: :editabilities
has_many :owned_files, through: :ownerships, source: :file
end
class File < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :ownerships, dependent: :destroy
has_many :users, through: :editabilities
has_one :owner, through: :ownerships, source: :user
end
class Editabilities < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
class Ownerships < ActiveReord::Base
belongs_to :user
belongs_to :file
end
How can I implement a feature like this?
The only problems I see here are the classes Editabilities and Ownerships. By Rails convention model class names should be singular, not plural.
class Editability < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
class Ownership < ActiveReord::Base
belongs_to :user
belongs_to :file
end
One way to quickly check your class names is by checking the result of the classify function:
> "editibilities".classify
=> "Editibility"
> "ownerships".classify
=> "Ownership"
The rest of the associations all look correct.
Your class should be named Ownership and not Ownerships.
ActiveRecord class names are normally singular and table names are plural.
Easier solution seems to add belongs_to association to File model. Because 1 File can have only 1 owner.
class File < ActiveRecord::Base
...
belongs_to :owner, class_name: 'User'
end
You will need to add owner_id column to files table.