I have in form this code line <%= f.collection_select :owner_ids, Owner.order(:id), :id, :name, {}, {multiple: true} %> that returns the list of owners in the massmedium form, but I need in this field also to include the companies too, so I could have in the same list the companies too, cause a massmedia chanel could be owned by a person or by a company.
Company.rb
class Company < ActiveRecord::Base
has_many :ownerships
has_many :massmedia, through: :ownerships
has_many :owners, through: :ownerships
end
Owner.rb
class Owner < ActiveRecord::Base
has_many :ownerships
has_many :massmedia, through: :ownerships
has_many :companies, through: :ownerships
end
Massmedium.rb
class Massmedium < ActiveRecord::Base
belongs_to :category
has_many :ownerships
has_many :owners, through: :ownerships
has_many :companies, through: :ownerships
end
= f.select :owners, options_for_select((Owner.all + Company.all)
.collect{|o| [o.name, "#{o.class},#{o.id}"], {multiple: true}
u have to define owners=(values) method in your model, which will parse values like ["Company,1","Owner,2"]
Related
I have 3 classes:
1. Article
class Article < ActiveRecord::Base
has_many :categories_articles
has_many :subcategories_articles
has_many :categories, :through => :categories_articles
has_many :subcategories, :through => :subcategories_articles
end
2.Category
class Category < ActiveRecord::Base
has_many :articles
has_many :categories_articles
has_many :categories_subcategories
has_many :subcategories, :through => :categories_subcategories
has_many :articles, :through => :categories_articles
end
3.The third class is the union of the first two, category_article
class CategoryArticle < ActiveRecord::Base
belongs_to :category
belongs_to :article
end
so, when i called in the view
<% f.collection_select(:category_ids, Category.all, :id, :name, {include_blank:"selects"},{class:'form-control select2 multi', :required=>true, multiple: true}) %>
I get this error:
uninitialized constant Article::CategoriesArticle
The same goes for the class Subcategory and subcategory_article
Try
has_many :category_articles
And
has_many :subcategory_articles
You'll also have to change these:
has_many :categories, :through => :categories_articles
has_many :subcategories, :through => :subcategories_articles
To something like:
has_many :categories, :through => :category_articles
has_many :subcategories, :through => :subcategory_articles
Rails doesn't pluralize both components of the composite table names. Just the last component.
I have 3 models set up as follows:
class User < ActiveRecord::Base
has_many :interests, as: :interesting, dependent: :destroy
has_many :games, through: :interests, source: :interesting, source_type: 'Game'
has_many :people, through: :interests, source: :interesting, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true
validates :interesting_id, presence: true
end
class Game < ActiveRecord::Base
has_many :users, through: :interests
has_many :interests, as: :interesting
end
class Person < ActiveRecord::Base
has_many :users, through: :interests
has_many :interests, as: :interesting
end
When I try to call user.games the SQL run on the database is
SELECT "games".* FROM "games"
INNER JOIN "interests"
ON "game"."id" = "interests"."interesting_id"
WHERE "interests"."interesting_id" = $1 AND
"interests"."interesting_type" = $2 AND
"interests"."interesting_type" = $3
[["interesting_id", 3],
["interesting_type", "User"],
["interesting_type", "Game"]]
so obviously nothing is returned. The query should work, as long as ["interesting_type", "User"] isn't included.
What am I doing wrong? What is the best way to set up the User class as well as the Game and Person class?
I'm using Rails v4.2.6
So to summarize, the following appears to work in this use case:
User < ActiveRecord::Base
has_many :interests, dependent: :destroy
has_many :games, through: :interests,
source: :interesting, source_type: 'Game'
has_many :people, through: :interests,
source: :interesting, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :user
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true
validates :interesting_id, presence: true
end
Please trying this
class User < ActiveRecord::Base
has_many :interests, dependent: :destroy
has_many :games, as: :interesting, through: :interests, source_type: 'Game'
has_many :people, as: :interesting, through: :interests, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true # I don't know the reason to use that if you use as polymorphic
validates :interesting_id, presence: true
end
The table_name interests it should have only the attributes :id:, :interesting_id, :interesting_type
in my app users has_many categories categories have sub_categories in db i create parent_id with main category id
and now i dont know how show main category if user select only sub_category
User.rb
has_many :users_ecategories
has_many :ecategories, through: :users_ecategories
Category.rb
class Ecategory < ActiveRecord::Base
has_many :users_ecategories
has_many :users, through: :users_ecategories
has_many :ecategories, class_name: 'Ecategory', foreign_key: 'parent_id'
end
Users_categories.rb
class UsersCategory < ActiveRecord::Base
belongs_to :user
belongs_to :ecategory
end
views/user/show.html.erb
<% #user.ecategories.each do |ecategory| %>
<%= ecategory.name %>
<%= ecategory.id %>
<% end %>
how to show main category name? <%= ecategory.parent_id.name %> doesn't work
please help
I think you need to define the parent relation
class Ecategory < ActiveRecord::Base
has_many :users_ecategories
has_many :users, through: :users_ecategories
has_many :ecategories, class_name: 'Ecategory', foreign_key: 'parent_id'
belongs_to :parent_ecategory, class_name: 'Ecategory', foreign_key: 'parent_id'
end
Then the call would be
<%= ecategory.parent_ecategory.name %>
I'm trying to add a new model to an existing model mesh. The existing one works perfectly but I can't get the new one to work properly and am wondering if the association is able to work the way I'm trying to make it work. Update: As I just got asked: belongs_to through was something I've read while gooling about the problem. If it doesn't exist, would has_one through be the correct way? I tried it as well but it also didn't work.
Here is the existing mesh:
class Course
has_many :users, through: :enrollments
has_many :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
# has fields :user_id, :course_id
end
Now a user should be able to rate a course he's completed. (If he has, there is an enrollment with his id and a course id.) I thought it would be best to write it as follows:
class Course
has_many :users, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
has_one :rating
# has fields :user_id, :course_id
end
class Rating
belongs_to :enrollment
belongs_to :course, through: :enrollment
belongs_to :user, through: :enrollment
end
When I try to create a Rating in the console, I get the following error:
User.first.ratings.create(text: "test", course_id: Course.first.id)
ArgumentError: Unknown key: through
Update
When I use has_one through insted, I get the following error:
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'User#ratings' because the source reflection class 'Rating' is associated to 'Enrolment' via :has_one.
Is it possible to do it this way at all? Thanks!
class Course
has_many :users, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
belongs_to :rating
# has fields :user_id, :course_id, rating_id
end
class Rating
has_one :enrollment
has_one :course, through: :enrollment
has_one :user, through: :enrollment
end
Note: Add foreignkey columns
And if you there is just one/two columns in ratings table merge them into enrollments like this.
class Course
has_many :users, through: :enrollments
has_many :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
# has fields :user_id, :course_id, rating-columns...
end
Structure
Maybe you're complicating this too much
class Enrollment
belongs_to :course
belongs_to :user
end
This means you have a join model which stores unique records, referencing both course and user. Your ratings are on a per user and course basis?
Why don't you just include rating as an attribute of your enrolment model?:
#enrolments
id | user_id | course_id | rating | created_at | updated_at
If you give rating a numeric value (1 - 5), it will give you the ability to rate the different enrolments like this:
user = User.first
course = Course.first
user.enrolments.create(course: course, rating: 5)
--
Ratings
This is, of course, based on your current model structure.
If you want to include ratings for courses by users (not tied to enrolment), you may wish to use a join model called course_ratings or similar:
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :enrolments
has_many :courses, through: :enrolments
has_many :ratings, through: :courses, class_name: "CourseRating"
end
#app/models/course.rb
Class Course < ActiveRecord::Base
has_many :enrolments
has_many :students, through: :enrolments, class_name: "User"
has_many :ratings, class_name: "CourseRating"
end
#app/models/course_rating.rb
Class CourseRating < ActiveRecord::Base
belongs_to :course
belongs_to :user
end
This will allow you to call:
user = User.first
user.courses.first.ratings
I have 2 applications one that serves as an API and has read only access and one that is the primary application. In the primary app, I have a has many through polymorphic relationship. The models in the main app look like so, and they work great:
class Category < ActiveRecord::Base
has_many :category_associations
has_many :posts, through: :category_associations
has_many :pages, through: :category_associations
end
class Post < ActiveRecord::Base
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :post
end
class Page < ActiveRecord::Base
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :post
end
class CategoryAssociation
belongs_to :category
belongs_to :associated, polymorphic: true
end
Now for the second app I will need to access the same tables but my class names will be different, this effects the type field that I cannot seem to override even with source_type.:
class Category < ActiveRecord::Base
has_many :category_associations
has_many :articles, through: :category_associations
has_many :static_contents, through: :category_associations
end
class Article < ActiveRecord::Base
self.table_name = 'posts'
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :article, source_type: 'Post'
end
class StaticContent < ActiveRecord::Base
self.table_name = 'pages'
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :static_content, source_type: 'Page'
end
class CategoryAssociation
belongs_to :category
belongs_to :associated, polymorphic: true
end
I get the following Error:
=> Posts.first.categories
# ActiveRecord::HasManyThroughAssociationPointlessSourceTypeError: Cannot have a has_many :through association 'Post#categories' with a :source_type option if the 'CategoryAssociation#category' is not polymorphic. Try removing :source_type on your association.
It also seems that when I grab the posts from the category
Have you tried putting polymorphic:true to the category belongs_to? It seems that's the direction pointed by the error message points, though I'm not sure
class CategoryAssociation
belongs_to :category, polymorphic: true
...
end