i have a custom validation method on user_calendar model:
class UserCalendar < ActiveRecord::Base
belongs_to :user
validate :should_match_company_calendars
...
private
def should_match_company_calendars
day_company_calendars = (..query..)
errors.add(:user, :company_calendar_match) if day_company_calendars.count < 1
end
And this is the message on en.yml:
company_calendar_match: "%{value.full_name} does not work in the selected time lapse"
Validation works fine but the message shows '%{value.full_name}' instead of the actual value for full_name. What am i missing?
You should call I18n.t method and pass value you want to show like below.
# model
errors.add(:user, I18n.t('... company_calendar_match', full_name: 'set value you want')) if day_company_calendars.count < 1
# en.yml
company_calendar_match: "%{full_name} does not work in the selected time lapse"
More detail is Rails Guides - i18n api features
Related
So having some troubles how to resolve my puzzle.
I'm having 2 models
1) Mode1.rb
class Model1 < ActiveRecord::Base
set_table_name "Model1"
set_sequence_name "Model1"
module Validate
def validate_discount
errors.add(:discount, "#blank") if discount.blank?
end
end
end
2) Model2.rb
class Model2 < ActiveRecord::Base
include Model1::Validate
validate :validate_discount
end
What i need? The trouble is that on submit page operating model2, so i need to execute validation from there to get proper error display, but as discount exists only in model1 i need to pass it to model2
The error what i get is now:
undefined local variable or method `discount' for #<Model2:0x12c952f8>
Might i need somehow pass it through the controller? I mean smth like this:
Model2.new
Model2["discount"] = 20
Model2.discount
I'm stuck now.
I think you can use attr_accessor for this purpose. With that you can set and get value of discount attribute.
class Model2 < ActiveRecord::Base
include Model1::Validate
attr_accessor :discount
validate :validate_discount
end
With this you can call:
model2 = Model2.new
model2.discount = params[:discount] #or whatever you set value for discount
And then validate it with your Model1::Validate module.
Im buliding an app with users model which can have different privileges, described by user_types model (id and type as a string). As I am running rails test, I am getting error
LoadError: Unable to autoload constant User_type, expected app/models/user_type.rb to define it
Here is my model:
class UserType < ActiveRecord::Base
belongs_to :user
validates :type, presence: true, length: { maximum: 100 }
end
Below is the controller and test file
class UserTypeController < ApplicationController
def index
#user_type = User_type.all
end
def new
#user_type = User_type.build(user_type_params)
end
private
def user_type_params
params.require(:user_type).permit(:type)
end
end
Testing model:
require 'test_helper'
class UserTypeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
def setup
#user_type = User_type.new(id:2,type:"test")
end
test "is_valid" do
assert #user_type.valid?
end
end
I wanted to do some basic "is_valid" test and I got an error described above. I have also dropped last model "user_type" and created it as "UserType" but It didn't work.
Throughout your controller and test you use User_type.all, User_type.build, User_type.new, but your model is named UserType.
Your model's class name is UserType. User_type is not defined anywhere. You either keep the model name as is and fix your test and controller, or rename the model name. The former is the Rails convention, so I'd suggest you go with that (the CamelCased name).
Change your model name to UserType as defined in class and from that UserType model's table remove type column or rename it to something like user_type since that is a reserved column name, can be used for single table inheritance only.
I have two models.
class User < ActiveRecord::Base
has_one :message
end
class Message < ActiveRecord::Base
belongs_to :user
end
If I have a created user with an associated Message and I delete that message and create a new one like, user.message returns nil. For example.
user = User.create
message = Message.create(user_id: user.id)
Message.where(user_id: user.id).destroy_all
Message.create(user_id: user.id)
# Now if I call this below, it always returns nil
user.message
Why does this occur? Shouldn't Rails 3 pick up on that change? How do I fix this?
Just load the object again before doing user.message like, user.reload.
reload - Reloads the record from the database.
I have a problem updating current_user attributes.
Here is my controller:
class BuildingsController < ApplicationController
def create
# ---- many_things
#current_user.user_info.money = 1000
current_user.user_info.update_attributes!(money: 100)
#current_user.user_info.money = 1000 --- why not 100?
# ---- many_things
end
end
And here is my model:
class User < ActiveRecord::Base
has_one :user_info
before_create :initialize_user
def initialize_user
self.create_user_info!(money:1000,level:1)
end
end
UserInfo model is blank:
class UserInfo < ActiveRecord::Base
end
update_attributes doesn't throw any exception, it seems that it doesn't save the record.
Any idea?
Thanks!
Are you trying to set money to $100 or $1,000? If looks like you are setting and resetting the money attribute at the same time.
When you call the code block in create if sets the value to $100. After that, in the model, you have an after callback :after_create that runs initialize_user, which sets the value of money to $1,000. Perhaps you want a before callback on create instead?
I have a model class as shown below:
class Product < ActiveRecord::Base
belongs_to :user;
attr_accessible :price
before_save :check_if_price_changed
after_save :notify_about_price_change
def check_if_price_changed
if (price.changed?) then
#price_changed = true
else
#price_changed = false
end
end
....
I want to record if the price of the product changed before I save it to the database. Then I have a routine that will do the notification once the product has been persisted successfully with the new price. But I get the following error when I try to check if the price attribute is dirty:
undefined method `changed?' for 20:Fixnum
Is that method not supported out of the box in rails 3.1? Am I calling it incorrectly or in the incorrect layer (model vs controller)?
This will return a boolean reflecting whether or not the price value has changed.
def check_if_price_changed
self.price_changed?
end
Have a look at the ActiveModel::Dirty documentation.