Nested attributes reject_if model has more than four children - ruby-on-rails

I have the below scenario:
class Question < ActiveRecord::Base
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :allow_destroy => true, :reject_if => ""
end
class Answer < ActiveRecord::Base
belongs_to :question
end
How can I reject the creation if one question has more than 4 answers?

I hope it work for you.
class Question < ActiveRecord::Base
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => -> {|q| q['answers'].count > 4}, :allow_destroy => true
end
class Answer < ActiveRecord::Base
belongs_to :question
end

Related

Best approach for creating a polling app in Rails

My models:
class Poll < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :description
has_many :questions, dependent: :destroy
has_many :responses, through: :questions
accepts_nested_attributes_for :questions, reject_if: lambda { |a| a[:title].blank? }, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :poll
has_many :answers, dependent: :destroy
has_many :responses, through: :answers
accepts_nested_attributes_for :answers, reject_if: lambda { |a| a[:content].blank? }, allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :question
has_many :responses
end
class Response < ActiveRecord::Base
belongs_to :answer
end
When you go to /polls/.:id it shows the poll with its corresponding questions and each question with its corresponding answers.
I've been playing around with this answer but I don't know what to do from there. I want whomever I send a poll link (/polls/.:id) to to be able to answer that poll. The method described in the answer linked before creates a form for each question.
I'd check out the documentation on rails views here:
http://guides.rubyonrails.org/action_view_overview.html
There is tons of documentation on how to access and transport data with various form elements.

Allow user to choose correct answer with accepts_nested_attributes_for?

I have the rails relations:
class Quiz < ActiveRecord::Base
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions,
reject_if: proc { |a| a[:content].blank? },
allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :quiz
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers,
reject_if: proc { |a| a[:content].blank? },
allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :question
end
How would a structure the model so when the user is in Quiz#edit or Quiz#new they can select which answer (with radio buttons) is the correct answer?
In the answers table, add an additional attribute called 'is_correct_answer' that will be true only for one combination.

how to show polymorphic record ruby on rails

I have 3 modules Blog, News, Article and I gathering reviews for all.
class Review < ActiveRecord::Base
attr_accessible :reviewable_type,:reviewable_id,:description,:context,:language,:status, :title, :user_id,:review_category_id,:ratings
belongs_to :user
belongs_to :review_category
belongs_to :reviewable, :polymorphic => true
class Blog < ActiveRecord::Base
attr_accessible :description, :status, :title
has_many :reviews, :as => :reviewable, :dependent => :destroy
class News < ActiveRecord::Base
attr_accessible :description, :status, :title
has_many :reviews, :as => :reviewable, :dependent => :destroy
class Article < ActiveRecord::Base
attr_accessible:description, :status, :title
has_many :reviews, :as => :reviewable, :dependent => :destroy
My question is, how to get all the reviews posted on current user's blog, article, news etc?
Just like how you associate other has_many relations. Like this:
current_user.blogs.first.reviews
current_user.articles.first.reviews
current_user.news.first.reviews
In your blog show page,
#blog.reviews.each |r|
r.review_text
end
Because you're trying to call for a user, I'd recommend looking at the User model too:
#app/models/user.rb
Class User < ActiveRecord::Base
#User can submit many reviews
has_many :reviews
#User can submit many blogs / news / articles
has_many :blogs
has_many :news
has_many :articles
def first_blog
blogs.first
end
end
#app/models/review.rb
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :review_category
belongs_to :reviewable, :polymorphic => true
end
#app/models/blog.rb
class Blog < ActiveRecord::Base
belongs_to :user
has_many :reviews, :as => :reviewable, :dependent => :destroy
def first_review
reviews.first
end
end
#app/models/news.rb
class News < ActiveRecord::Base
belongs_to :user
has_many :reviews, :as => :reviewable, :dependent => :destroy
end
#app/models/article.rb
class Article < ActiveRecord::Base
belongs_to :user
has_many :reviews, :as => :reviewable, :dependent => :destroy
end
Reviews
This will allow you to call:
#########
#Reviews#
#########
#Submitted
current_user.reviews
#Received
current_user.first_blog.first_review
current_user.first_blog.reviews

Rails3 has_many through working on local but fail on heroku

i'm using sqlit3 for local and Postgre for heroku.
Everything works fine until i upload my files to heroku. Here is my model.
class User < ActiveRecord::Base
belongs_to :unit
has_friends
end
class Unit < ActiveRecord::Base
attr_accessible :unit, :floor
has_many :users
belongs_to :block
end
class Block < ActiveRecord::Base
attr_accessible :block, :units_attributes
has_many :units, :dependent => :destroy
accepts_nested_attributes_for :units, allow_destroy: true
belongs_to :postalcode
end
class Postalcode < ActiveRecord::Base
attr_accessible :postalcode, :blocks_attributes
has_many :blocks, :dependent => :destroy
accepts_nested_attributes_for :blocks, allow_destroy: true
belongs_to :neighbourhood
end
class Neighbourhood < ActiveRecord::Base
attr_accessible :name, :streetname, :postalcodes_attributes
has_many :postalcodes, :dependent => :destroy
has_many :blocks, :through => :postalcodes
has_many :units, :through => :blocks
has_many :users, :through => :units
accepts_nested_attributes_for :postalcodes, allow_destroy: true
validates :name, :presence => true
validates :streetname, :presence => true
end
i troubleshooted and found that the problem is with this method in the controller.
#neighbours = current_user.unit.block.postalcode.neighbourhood.users
Although #neighbours = current_user.unit.block.postalcode.neighbourhood works perfectly fine.
Please help, i'm desperate, i have tried googling for it one whole day.
Check out this answer to a similar issue
It is quite likely the error is coming up from WHERE "postalcodes"."neighbourhood_id" = 1 which indicates that neighbourhood_id in postalcodes table is created as a String, instead of an integer.
Follow the steps mentioned in the answer accordingly, and change it to an Integer.

has_one :through polymorphic - is it possible?

I have models in my app:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Project < ActiveRecord::Base
has_many :discussions, :dependent => :destroy
has_many :tickets, :dependent => :destroy
end
class Discussion < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
class Ticket < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
Everything works fine, but sometimes it's not very convinient to get project from comment through commentable, i.e. comment.commentable.project.
Is there any way to make has_one project in Comment model?
I would add the following method to your class Comment:
def project
self.commentable ? self.commentable.project : nil
end
This will give you the same result without all the magic of ActivRecord.

Resources