I have a Project model, which has many posts and tasks. Both posts and tasks have many attachments. Attachment is polymorphic. How can I query all the attachments for a single project?
Obviously this doesn't work because we don't have an attachable table; we have posts and tasks. It also produces a can't eager load polymorphic association 'attachable'.
# project.rb
def attachments
Attachment.joins(:attachable).where('attachable.project_id = ?', id)
end
Rest of code:
class Project < ActiveRecord::Base
has_many :posts
has_many :tasks
end
class Post < ActiveRecord::Base
belongs_to :project
has_many :attachments, as: :attachable
end
class Task < ActiveRecord::Base
belongs_to :project
has_many :attachments, as: :attachable
end
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
end
Related
So I have models like this:
folder, up_file has 1 chat_room
chat_room belongs to crmable with polymorphic: true (so 1 chat_room may belongs to folder or 1 up_file)
chat_room has many comments
comment belongs to chat_room and user (who posted the comment)
I've defined all relationship like this:
class Folder < ApplicationRecord
has_one :chat_room, as: :crmable, dependent: :destroy
end
class UpFile < ApplicationRecord
has_one :chat_room, as: :crmable, dependent: :destroy
end
class ChatRoom < ApplicationRecord
belongs_to :crmable, polymorphic: true
has_many :comments, dependent: :destroy
has_many :users, through: :comments
end
class Comment < ApplicationRecord
belongs_to :chat_room
belongs_to :user
end
This setup worked fine with my web app. However, I'm writing API for this app, and I can't get that nested associations serialized with this gem
https://github.com/rails-api/active_model_serializers
I've follow their tutorial here but my associations not rendered.
class FolderSerializer < ActiveModel::Serializer
attributes :id, :name, :ancestry, :creator_name #... my other attributes
has_one :chat_room, include: [ :id, comments: :user ]
# ^ I tried to serialize folder's chat_room with all it comments, along with these comments' users.
end
All I got is this - chat_room association not serialized?
I am contributing one of the ruby on rails application over GitHub where I faced the following scenario:
I am having following models which I want to convert to make polymorphic:
class Comment < ActiveRecord::Base
belongs_to :team
belongs_to :user
belongs_to :application
belongs_to :project
end
class Team < ActiveRecord::Base
has_many :comments
end
class Project < ActiveRecord::Base
has_many :comments, -> { order('created_at DESC') }, dependent: :destroy
end
class User < ActiveRecord::Base
end
class Application < Rails::Application
end
I made following changes to make it polymorphic:
Perform database change to removed team_id, project_id, application_id and user_id and added commentable_id and commentable_type to comments table.
Modifications in models as described within rails guides.:
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
class Team < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Project < ActiveRecord::Base
has_many :comments, as: :commentable, -> { order('created_at DESC') }, dependent: :destroy
end
While I use it with default scope, It doesn't allow me to use with default scope and gives error with below line:
has_many :comments, as: :commentable, -> { order('created_at DESC') }, dependent: :destroy
I am confused to change in following models:
class User < ActiveRecord::Base
end
class Application < ActiveRecord::Base
end
Should I need following changes in User and Application model?
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Application < ActiveRecord::Base
has_many :comments, as: :commentable
end
Thanks in Advance!
if your user/application object needs comments then add
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Application < ActiveRecord::Base
has_many :comments, as: :commentable
end
else create belongs_to/has_many relationship not polymorphic
Eg.
class User < ActiveRecord::Base
has_many :comments
end
class Application < ActiveRecord::Base
has_many :comments
end
Here is my models:
class User < ActiveRecord::Base
has_many :products
has_many :comments
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
I need to get comment records from current user products only
How do I do that? thanks
If we move the relationships to use a has_many: comments, through: products you can probably get what you're after:
class User < ActiveRecord::Base
has_many :products
has_many :comments, through: products
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
Now you can do user.comments.
The rails docs are here, which say:
A has_many :through association is often used to set up a many-to-many
connection with another model. This association indicates that the
declaring model can be matched with zero or more instances of another
model by proceeding through a third model. For example, consider a
medical practice where patients make appointments to see physicians.
class User < ActiveRecord::Base
has_many :posts
has_many :images, as: :imageable
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :images, as: :imageable
end
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
Is there a specific way where I can do User.images and get both the user's images and the post's images that belong to that user?
For some reason I can't wrap my head around how to do this best.
In that case you can avoid polymorphic relationship, simple has_many and belongs_to will suffice. where :
class User ActiveRecord::Base
has_many :posts
has_many :images
end
class Post ActiveRecord::Base
belongs_to :user
has_many :images
end
class Image < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
But then again I think you wanted to ask about User.last.images and not User.images
the same can be done through has_many through associations as well
I hope that someone can help me with importing csv file into nested resource model.
I have a couple of models (Rails 4.0, Ruby 2.0):
class Level < ActiveRecord::Base
belongs_to :level_type
has_many :project_levels
has_many :projects, through: :project_levels
end
class LevelType < ActiveRecord::Base
has_many :levels
end
class ProjectLevel < ActiveRecord::Base
belongs_to :level
belongs_to :project
end
class Project < ActiveRecord::Base
belongs_to :company
has_many :project_levels
has_many :levels, through: :project_levels
end
class Company < ActiveRecord::Base
has_many :projects
end
And what I do similar to this in a rake task:
company.projects.create(company_name: row[3])
level_type.levels.create(position: row[4])
My problems is how to create levels from companies? Is this the right thing to do?
company.projects.levels.create(company_name: row[3])
What is confusing me is that I have 2 sides to get to the levels and the same information I have to put into it (company_name: row[3]) and I am not sure how to deal with it. Any help would be appreciated.