How to fix this error "HasManyThroughCantAssociateThroughHasOneOrManyReflection" in rails 5? - ruby-on-rails

I Have created simple rails apps and I got error "HasManyThroughCantAssociateThroughHasOneOrManyReflection" that shown in rails admin when I want to post an Image.
this my code:
can anyone Help me to solve it ?
class ProductInvest < ApplicationRecord
belongs_to :product
has_many :pictures, through: :product
end
class Product < ApplicationRecord
has_many :pictures
has_many :Product_invests
end
class Picture < ApplicationRecord
belongs_to :product
has_many :Product_invests, through: :product
end

Your associations aren't setup correctly. Try this instead:
class ProductInvest < ApplicationRecord
has_many :products
has_many :pictures, through: :products
end
class Product < ApplicationRecord
belongs_to :pictures
belongs_to :product_invests
end
class Picture < ApplicationRecord
has_many :products
has_many :product_invests, through: :products
end
You can get more information here: RubyGuides

Related

Many-To-Many Association Query

I have the following models:
class Restaurant < ApplicationRecord
has_one_attached :image
has_many :categories, through: :restaurant_category
end
class Category < ApplicationRecord
has_many :restaurants, through: :restaurant_category
end
class RestaurantCategory < ApplicationRecord
belongs_to :restaurant
belongs_to :category
end
I would query in one shot all the categories associated to a restaurant. SOmething like this:
a = Restaurant.find(1)
a.restaurant_category
But I have:
NoMethodError (undefined method `restaurant_category' for #<Restaurant:0x00007f5214ad2240>)
How can I solve it?
This:
class Restaurant < ApplicationRecord
has_one_attached :image
has_many :categories, through: :restaurant_category
end
... should look like this:
class Restaurant < ApplicationRecord
has_one_attached :image
has_many :restaurant_categories
has_many :categories, through: :restaurant_categories
end
Similarly, this:
class Category < ApplicationRecord
has_many :restaurants, through: :restaurant_category
end
... should look like this:
class Category < ApplicationRecord
has_many :restaurant_categories
has_many :restaurants, through: :restaurant_categories
end
Which you would use like:
restaurant_categories = Restaurant.find(1).categories
This is all explained in the has_many :through section of the Active Record Associations guide.

How to setup model associations in Rails

I'm trying to create associations for three models in my Rails application. In the application a User can access courses which have videos. How would I model this?
This is what I currently have:
class User < ApplicationRecord
has_many :courses
has_many :videos, through: :courses
end
class Course < ApplicationRecord
belongs_to :user
has_many :videos
end
class Video < ApplicationRecord
belongs_to :course
belongs_to :user
end
Is this the correct way to model these associations for what I want the application to be able to achieve?
Normally, this would look something like:
class UserCourse < ApplicationRecord
belongs_to :user
belongs_to :course
end
class User < ApplicationRecord
has_many :user_courses
has_many :courses, through: :user_courses
has_many :videos, through: :courses
end
class Course < ApplicationRecord
has_many :user_courses
has_many :users, through: :user_courses
has_many :videos
end
class Video < ApplicationRecord
belongs_to :course
has_many :users, through: :course
end
That should let you do:
#user.courses
#user.videos
#course.users
#course.videos
#video.course
#video.users
(Assuming, of course, you've instantiated each of the above variables and you have associated records.)

Ruby on Rails: Polymorphic Association Confusion

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

Retrieve polymorphic associations in Rails from one model

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

Modeling "Likes" in rails HABTM vs HM/BT

What would be the best method to model "likes" in rails for my app. I could either to the following:
class User < ActiveRecord::Base
has_many :things
has_many :likes
has_many :liked_things, through: :likes, source: :thing
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :thing
end
class Thing < ActiveRecord::Base
belongs_to :user
has_many :likes
has_many :liking_users, through: :likes, source: :user
end
Or
class User < ActiveRecord::Base
has_many :things
has_and_belongs_to_many :things
end
class Thing < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :users
end
What approach would be best and why? I plan to have an activity feed in my app as well, if that helps determine the best approach.
The answer to this question depends on whether or not Like will ever have any attributes or methods.
If its only purpose of existence is to be the HABTM relationship between Users and Things, then using the has_and_belongs_to_many relationship would suffice. In your example, having has_many and belongs_to is redundant. All you would need in this case is:
class User < ActiveRecord::Base
has_and_belongs_to_many :things
end
class Thing < ActiveRecord::Base
has_and_belongs_to_many :users
end
On the other hand, if you anticipate that a Like will have an attribute (e.g. maybe someone will really like something, or love it, etc.) then you can do
class User < ActiveRecord::Base
has_many :likes
has_many :liked_things, through: :likes, source: :thing
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :thing
end
class Thing < ActiveRecord::Base
has_many :likes
has_many :liking_users, through: :likes, source: :user
end
Note that I removed has_many :things and belongs_to :user as they are redundant.

Resources