Ruby on rails Query related - ruby-on-rails

I have 2 Models
FOLDERS
:name
:parent_id
AUTHFOLDERS
:folder_id
:company_id
:user_id
:role #for sharing
class Folder < ActiveRecord::Base
has_many :authfolders
has_many :companies, :through => :authfolders
accepts_nested_attributes_for :authfolders
validates :name, presence: true
end
class Authfolder < ActiveRecord::Base
belongs_to :company
belongs_to :folder
end
How can select "folders" where Authfolders.company_id = x?

You can join with your related table and then set your condition:
Folder.joins(:authfolders).where('authfolders.company_id = :company_id', company_id: x)

Related

Rails 5 Active Model Serializer display has many conditions in JSON

In my project I have those models and relationships:
class Course < ApplicationRecord
has_many :segments, inverse_of: :course, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :segments, :allow_destroy => true
end
class Segment < ApplicationRecord
validates :data ,presence: true, if: :segment_is_video?
validates :segment_type ,presence: true
validates_presence_of :course
belongs_to :course, inverse_of: :segments
has_many :questions
accepts_nested_attributes_for :questions, :allow_destroy => true
def segment_is_video?
segment_type == 'Video'
end
end
class Question < ApplicationRecord
validates_presence_of :segment
belongs_to :segment, inverse_of: :questions
end
I want to display the data field only if the type is Video, and I want to display questions array only if type field is Quiz. I'm using Active Model Serializer for that but it's not working. the data still displaying when the type is Quiz and the question array doesn't show at all.
this is my code for the serializers:
class CourseSerializer < ActiveModel::Serializer
attributes :id, :title, :author
has_many :segments
def root
'Course'
end
end
class SegmentSerializer < ActiveModel::Serializer
attributes :id, :unit_id, :unit_title, :name, :segment_type, :data
belongs_to :course
has_many :questions, if: -> { isQuiz }
def root
'Segments'
end
def include_data?
object.segment_type == 'Video'
end
def isQuiz
object.segment_type == 'Quiz'
end
end
class QuestionSerializer < ActiveModel::Serializer
attributes :id, :question, :answer1, :answer2, :answer3, :answer4, :correct
belongs_to :segment
def root
'Question'
end
end

Rails sum Expenses which belongs to Projects and Users

Given is a User Model who has_many Projects.
each Projects belongs_to a User
joined by a Assigned_projects Model
each Project has_many Expenses which belongs to a Project and a User
User Model:
class User < ActiveRecord::Base
has_many :assigned_projects
has_many :projects, :through => :assigned_projects
has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id
end
Project Model:
class Project < ActiveRecord::Base
belongs_to :user
has_many :assigned_projects
has_many :users, :through => :assigned_projects
belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
has_many :expenses
end
join Model:
class AssignedProject < ActiveRecord::Base
attr_accessible :project_id, :user_id, :position, :project
belongs_to :user, class_name: "User"
belongs_to :project, class_name: "Project"
validates :user_id, presence: true
validates :project_id, presence: true
end
Expenses Model:
class Expense < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
Sum all Expenses of a project is easy:
#current_project.expenses.sum(:amount)
I want to list the sum of all Expenses each User added.
The Question is how to sum all Expenses for each user who is assigned to the Project.
I'am looking for something like:
#current_user.#current_project.expenses.sum(:amount)
I assume you are trying to do the following. If not, please explain your question a bit more.
#current_project.expenses.where(user_id: #current_user.id).sum(:amount)

how to List date in order of number of another data?

class Subject < ActiveRecord::Base
attr_accessible :name, :teacher_id
has_and_belongs_to_many :courses
belongs_to :teacher
has_many :users, :through =>:feedback
has_many :feedbacks
end
class Course < ActiveRecord::Base
attr_accessible :name
has_many :users
has_and_belongs_to_many :subjects
validates :name, presence: true, length: { maximum: 50 }
end
class Feedback < ActiveRecord::Base
attr_accessible :rating, :recommendations, :strengths, :subject_id, :user_id, :weaknesses
belongs_to :user
belongs_to :subject
end
class User < ActiveRecord::Base
attr_accessible :course_id, :email, :gender, :name, :password, :password_confirmation
has_secure_password
belongs_to :course
has_many :feedbacks
has_many :subjects, :through =>:feedback
end
There is a join table between course and subjuct
I am a student and freshman in rails,I have some problem in this final project
How to
List subjects in order of number of feedbacks.
List students with no feedbacks.
I have no idea how to do it. Thanks very much
I am providing solution for two queries you need:
List subjects in order of number of feedbacks
#subjects = Subject.find(:all, :include=>:feedbacks).sort_by { |p| p.feedbacks.size}
List students with no feedbacks
User.find(:all, :include => :feedbacks, :conditions => "feedbacks.id is null")

Rails 'DB - no such column' error for has_many through relationship

I created a has_many :through relationship for a rails app I am creating. However, I am unable have nested attributes for the many to many relationship.
My models are as follows:
article.rb
class Article < ActiveRecord::Base
attr_accessible :body, :title
has_many :article_tags
has_many :tags, :through => :article_tags, :foreign_key => :tag_id
accepts_nested_attributes_for :tags, reject_if: :all_blank
attr_accessible :tags_attributes
validates_presence_of [:body, :title]
end
tag.rb:
class Tag < ActiveRecord::Base
has_many :article_tags
has_many :articles, :through => :article_tags, :foreign_key => :article_id
attr_accessible :name
validates_presence_of :name
accepts_nested_attributes_for :articles, reject_if: :all_blank
attr_accessible :articles_attributes
end
article_tag.rb:
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
Now in my view for the article (show.html.slim), I want to show all tags that belong to the article using the line:
- #article.tags.each do |t|
= t.name
However, I get the error:
SQLite3::SQLException: no such column: article_tags.article_id: SELECT "tags".* FROM "tags" INNER JOIN "article_tags" ON "tags"."id" = "article_tags"."tag_id" WHERE "article_tags"."article_id" = 1
This problem is solved.
For anybody who needs to know:
You must manually add the foreign keys in the migration and the schema. I added them and voila! it works!
Thanks to Dan Reedy for his help!
Your ArticleTag class is incorrect. The belongs_to associations should be singular.
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
You must use has_and_belongs_to_many in this situation.

Join model not getting created properly using accepts_nested_attributes_for

I have a User model and a Project model joined with the Ownership model using has_many :through. I am trying to set an Ownership attribute's value as I create an association between a User and a Project by using accepts_nested_attributes_for.
Here is my User model:
class User < ActiveRecord::Base
attr_accessible :name, :email, :admin, :projects
has_many :ownerships
has_many :projects, :through => :ownerships
accepts_nested_attributes_for :projects
Here is my ownership model:
class Ownership < ActiveRecord::Base
attr_accessible :owner_type
belongs_to :project
belongs_to :user
validates :user_id, :presence => true
validates :project_id, :presence => true
validates :owner_type, :presence => true
end
and my project model:
class Project < ActiveRecord::Base
attr_accessible :name, :description
has_many :ownerships
has_many :users, :through => :ownerships
and this is how I'm trying to set the owner_type value of the ownership model as its being created:
current_user.ownerships.create(:owner_type => 'designer', :project => #project)
for some reason this isn't creating the ownership join model or (obviously) setting the owner_type value of the ownership model. What can I do to fix this?

Resources