Conditional check in rabl - ruby

I have question model with has many relationship of question_options.
class Question < ActiveRecord::Base
#relationship
belongs_to :user
has_many :question_options, :dependent => :destroy,:conditions => "is_deactivated is FALSE"
Question option
class QuestionOption < ActiveRecord::Base
attr_accessible :question_id,:option,:order,:is_other,:is_deactivated
belongs_to :question
In my question_detail rabl I have
object #question
attributes :id, :status
child :question_options do
attributes :question_id,:option,:order,:is_other
end
Here I want to respond only the question_option which has is_other = false
like the below....
object #question
attributes :id, :status
child :question_options do
attributes :question_id,:option,:order,:is_other = true
end
How do I check a condition in rable that?

One easy way to do this is to use if if option on attributes, for example:
object #question
attributes :id, :status
child :question_options do
attributes :question_id,:option,:order,:is_other,
:if => lambda { |question_option| question_option.is_other }
end
Also check out the section on conditions.

Related

Value of key for Model object gained dynamically

I'm trying to gain a value from an associated table linked via a field in a table using the following (where i is created in an each loop):
value = current_user.question["convq#{i}"].id
My table has references from the Questions table to a Questionsbank table in the form of columns named convq1_id, convq2_id ... convq3_id.
I simply want to cycle through each field in Questions and gain the questionbank.text field.
Sadly, the above will only give me the physical value in the question table field not a link to the associated record using the following code:
(1...9).each do |i|
value = current_user.question["convq#{i}"].id
end
Can anyone let me know how I can cycle through table and gain the associated 'id' or text value of the referenced record?
Thanks in advance.
Each model is set up as:
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :convq1, :class_name => 'Questionbank'
belongs_to :convq2, :class_name => 'Questionbank'
belongs_to :convq3, :class_name => 'Questionbank'
belongs_to :convq4, :class_name => 'Questionbank'
belongs_to :convq5, :class_name => 'Questionbank'
belongs_to :convq6, :class_name => 'Questionbank'
belongs_to :convq7, :class_name => 'Questionbank'
belongs_to :convq8, :class_name => 'Questionbank'
belongs_to :convq9, :class_name => 'Questionbank'
has_many :questionbanks
end
and ...
class Questionbank < ActiveRecord::Base
has_many :likes
has_many :questions
end
I think this is what you might be looking for:
(1...9).each do |i|
value = current_user.question["convq#{i}"]
questionbank = Questionbank.find_by( question: "{value}")
questionbank.id
end

Update multiple records using one form

I have a following situation: user has multiple assets and each asset has one asset_detail record.
Models:
class User < ActiveRecord::Base
has_many :assets
end
class Asset < ActiveRecord::Base
has_one :asset_detail
belongs_to :user
accepts_nested_attributes_for :asset_detail,
:allow_destroy => false
attr_accessible # ...
end
class AssetDetail < ActiveRecord::Base
belongs_to :asset
attr_accessible # ...
end
Controller actions:
def edit
#user = current_user
#assets = Asset.all
end
def update
#user = current_user
#user.update_attributes(params["user"])
end
View:
= form_for #user, url: 'update action url' do |f|
= f.fields_for :assets do |ff|
= ff.text_field :title
= ff.fields_for :asset_detail do |fff|
= fff.text_field :value
The problem is that all the form fields are populated properly but i'm not able to save them. The form is sent without any error but the data is not updated.
I think your models should look like this:
class User < ActiveRecord::Base
attr_accessible :assets_attributes #...
has_many :assets
accepts_nested_attributes_for :assets_attributes
end
class Asset < ActiveRecord::Base
attr_accessible :asset_detail_attrbutes # ...
has_one :asset_detail
belongs_to :user
accepts_nested_attributes_for :asset_detail_attributes,
:allow_destroy => false
end
the reason being, you need to be able to set the attributes via the attributes hash passed to each model. HTH!

Why is assign_attributes with array of ids for has_many association only associating one object?

I have a form where users can upload assets using ajax. This creates many Asset objects that I then want to associate with a Post object when it is created. I have a form_field named asset_ids that I update with the created Asset ids as they are created. When I create the Post object and populate its data with assign_attributes, only ONE association is created, no matter how many ids are there.
Asset model:
class Asset < ActiveRecord::Base
attr_accessible :caption, :image
belongs_to :post
has_attached_file :image, :styles => { :large => "600x600>", :medium => "300x300>", :thumb => "100x100>" }
end
Post model:
class Post < ActiveRecord::Base
attr_accessible :content, :post_date, :status, :title, :tag_list, :asset_ids, :as => :admin
has_many :assets, :dependent => :destroy
has_and_belongs_to_many :tags
validates :content, :post_date, :title, :presence => true
end
An example of the posted data hash:
{"title"=>"Test post", "status"=>"true", "post_date"=>"01/02/2013", "content"=>" Some content", "tag_list"=>"", "asset_ids"=>"97,102"}
The example above assigns only one Asset (id 97) to the new Post, when I assign_attributes like so:
#post = Post.new
#post.assign_attributes params[:post], :as => :admin
I had to make sure I was assigning the ids as an array:
#post.asset_ids = params[:post][:asset_ids].split(",")

has_many :through child and parent validations

I have a rails 3.0
has_many :X, :through => :something set up and i have a custom validator that does some custom validation on some complicated logic. I want to when you add anything to this many to many relationship both models are valid?
Project Class:
class Project < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
validates :name, :presence => true
end
Assignment:
class Assignment < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
User class with custom validator:
class MyCustomValidator < ActiveModel::Validator
def validate( record )
if record.projects.length > 3
record.errors[:over_worked] = "you have to many projects!"
end
end
end
class User < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
validates :name, :presence => true
validates_with MyCustomValidator
end
What i really want to do is prevent each model from invalidating the other so to say
prevent
my_user.projects << fourth_project
and
my_project.users << user_with_four_projects_already
from happening. Right now it allows the assignment and just the user becomes invalid.
class Project < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
validates :name, :presence => true
validates_associated :users
end
According to the docs, users must already be assigned to Projects in order to be validated. So:
my_user.projects << fourth_project
would occur, then projects would validate the user and see that it is indeed invalid, making the project invalid as well as in the inverse example.

rails belong_to which class to choose

There is a model relation like this.
class A
belongs_to :ref_config,:class_name => 'User'
end
My question is :
the A has a attribute named flag, now i want to create a function like this:
if flag == 1, I want the class A like this belongs_to :ref_config,:class_name => 'Department and if flag == 2, i want the class A like this belongs_to :ref_config,:class_name => 'User'
How can I implement the function
Thank you!
Have a look at polymorphic associations, which will let you use the same belongs_to relation to refer to different models.
You could configure your models something like this:
class A < ActiveRecord::Base
belongs_to :ref_config, :polymorphic => true
end
class Department < ActiveRecord::Base
has_many :as, :as => :ref_config
end
class User < ActiveRecord::Base
has_many :as, :as => :ref_config
end
To set up the needed columns in the A table, use a migration like this:
class CreateAs < ActiveRecord::Migration
def self.up
create_table :as do |t|
t.string :name # or whatever other attributes A should have
t.references :ref_config, :polymorphic => true
end
end
def self.down
drop_table :as
end
end
From the little i got your question following may help you.
class A
belongs_to :department_config, :class_name => 'Department', :conditions=> flag= 1
belongs_to :user_config, :class_name => 'User', :conditions=> flag= 2
end

Resources