I Having an issue when trying to update a model that has has_and_belongs_to_many association.
Let's say that Post has_and_belongs_to_many Tag, and Post validates the presence of title and Tags.
If I update Post, removing its title and tags, I get validation error in title and tags, ok.
But ActiveAdmin already removed the records that make association between Post and Tag, so, if I leave Post edit page, the post is left invalid on database, without tags.
Here my models:
class Tag < ActiveRecord::Base
attr_accessible :label
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
attr_accessible :content, :title, :tag_ids
has_and_belongs_to_many :tags
validates_presence_of :content, :title, :tags
end
ActiveAdmin.register Post do
form do |f|
f.inputs do
f.input :title
f.input :content
f.input :image
f.input :tags
end
f.buttons
end
end
I usign chosen-rails gem and it allows user to unselect all tags of post.
Summarizing, my problem is: ActiveAdmin updates relationships on database before perform model validations.
There a solution for this behavior or I doing something wrong?
Edit:
Here the request log when I trying to update post without title and tags:
Started PUT "/admin/posts/8" for 127.0.0.1 at 2013-04-01 10:32:07 -0300
Processing by Admin::PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"amSbLlP/rgDrNn/N8lgq/KEaRXK1fMPShZDwpZ0QIJ4=", "post"=>{"title"=>"", "content"=>"content", "tag_ids"=>["", ""]}, "commit"=>"Update Post", "id"=>"8"}
AdminUser Load (0.2ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 LIMIT 1
Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8
(0.1ms) BEGIN
SQL (12.3ms) DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = 8 AND `posts_tags`.`tag_id` IN (1, 2)
(49.6ms) COMMIT
(0.1ms) BEGIN
(0.2ms) ROLLBACK
Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
Tag Load (0.2ms) SELECT `tags`.* FROM `tags`
Rendered /home/rodrigo/.rvm/gems/ruby-1.9.3-p125#blog/gems/activeadmin-0.5.1/app/views/active_admin/resource/edit.html.arb (192.3ms)
Completed 200 OK in 276ms (Views: 194.8ms | ActiveRecord: 63.3ms)
EDIT 2:
Ok, I sure that ActiveAdmin has this bug.
Looking at ActiveRecord behaviour, I think that validation flow is broken using only model class. See this example:
1.9.3p125 :064 > post = Post.find(8)
Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
=> #<Post id: 8, title: "title", content: "content", created_at: "2013-03-27 13:13:20", updated_at: "2013-03-27 13:13:20", image: "extrato.bmp">
1.9.3p125 :065 > post.tags
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8
=> [#<Tag id: 1, label: "tag", created_at: "2013-02-25 18:32:45", updated_at: "2013-02-25 18:32:45">, #<Tag id: 2, label: "new", created_at: "2013-02-25 18:32:50", updated_at: "2013-02-25 18:32:50">]
1.9.3p125 :066 > post.title = ""
=> ""
1.9.3p125 :067 > post.save #<<<<<<< It's invalid on title
=> false
1.9.3p125 :068 > post.tags = [] #<<<<<<< This shouldnt trigger database update
(0.3ms) BEGIN
SQL (0.5ms) DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = 8 AND `posts_tags`.`tag_id` IN (1, 2)
(55.5ms) COMMIT
=> []
1.9.3p125 :069 > post.save #<<<<<<< It's invalid on title AND TAGS
(0.2ms) BEGIN
(0.2ms) ROLLBACK
=> false
1.9.3p125 :070 > post.reload
Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1
=> #<Post id: 8, title: "title", content: "content", created_at: "2013-03-27 13:13:20", updated_at: "2013-03-27 13:13:20", image: "extrato.bmp">
1.9.3p125 :071 > post.valid? #<<<<<<< Now, I have this model in invalid state
Tag Load (0.6ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8
=> false
Has any way to update post attributes(including tags) and validate the model before doing any database update?
You can use this gem: https://github.com/MartinKoerner/deferred_associations
The deferred associations will fix this bug.
#Rodrigo I was able to reproduce your issue locally without active admin, the issue is actually that is one of the default operations when using HABTM relationships if you see [here][1]
[1]: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many it says:
collection=objects
Replaces the collection’s content by deleting and adding objects as appropriate.
So apparently you will need to override this operation
Here is an example:
Override ActiveRecord << operator on has_many :through relationship, to accept data for the join model
Let me know how can I help you
I'd propose to add a tmp variable and store values in it. Then you should move them to database if validation passed.
Here is an example:
Your models/article.rb:
class Article < ActiveRecord::Base
validate :author_presence
has_and_belongs_to_many :authors
attr_writer :tmp_author_ids
def tmp_author_ids
#tmp_author_ids || author_ids
end
def author_presence
if tmp_author_ids.reject(&:blank?).empty?
errors.add(:tmp_author_ids, 'Author is missing')
else
self.author_ids = tmp_author_ids
end
end
end
Your admin/article.rb, block form:
f.input :tmp_author_ids, as: :select, multiple: true, collection: Author.all, label: 'Authors'
That's it
So, for anyone having the same issue, I found a workaround:
You can define a before_remove in the has_and_belongs_to_many association and raise an exception that then you can catch in ActiveAdmin.
For this it would be
class Post < ActiveRecord::Base
attr_accessible :content, :title, :tag_ids
has_and_belongs_to_many :tags, before_remove: :check_something
validates_presence_of :content, :title, :tags
end
def check_something(agent)
if self.tags.size == 1
raise ActiveModel::MissingAttributeError.new 'something'
end
end
more info: https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
if you want, you can redefine you update action in active_admin to prevent saving empty tags, in something like this style
ActiveAdmin.register Post do
controller do
def update
if params[:post][:tag_ids] == ["", ""]
flash.now[:alert] = "You can't remove all tags"
render :edit
else
super
end
end
end
...
end
and i think this stuff from model can be deleted
attr_accessor :new_tag_ids
validate :validate_new_tags_ids
after_save :update_tags
def update_tags
self.tag_ids = #new_tag_ids if defined?(#new_tag_ids)
#new_tag_ids = nil
end
private
def validate_new_tags_ids
errors[:tags] << "can't be blank (2)" if #new_tag_ids.blank?
end
Related
Rails 5.2
I have a partial:
views/authors/_add_author_comment.html/slim
= form_for :author_note, url: author_notes_url, method: :post do |f|
= f.hidden_field :author, value: #book.author
= f.text_area :comment
button.btn.btn-primary type="button"
= f.submit t('authors.show.submit_comment')
In my controllers/author_notes_controller.rb, I have:
def create
#author_note = AuthorNote.new(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
#author_note.save
end
When the form displays (part of a larger view), and I fill the comment out, and click on "Submit Comment", the comment is not saved. In the console, I see the following:
Processing by AuthorNotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Jju1cpsLjXLY/TaF9p/Zkh8JQ/+KajjxwQHgNU4tNU9bjL8BiZQ8xL3S7ske1KqflOPHVaB9UTWRvgxNqzLd7Q==", "author_note"=>{"author"=>"John Dow", "comment"=>"This is a test"}, "commit"=>"Save Note"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = '5' ORDER BY `users`.`id` ASC LIMIT 1
↳ app/controllers/author_notes_controller.rb:23
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = '5' LIMIT 1
↳ app/controllers/author_notes_controller.rb:23
(0.2ms) BEGIN
↳ app/controllers/author_notes_controller.rb:28
(0.1ms) ROLLBACK
↳ app/controllers/author_notes_controller.rb:28
No template found for AuthorNotesController#create, rendering head :no_content
Completed 204 No Content in 60ms (ActiveRecord: 10.6ms)
Why is it ActiveRecord rolling back, and not saving the note to the author_notes table?
Resolution:
the author_note.rb model, I had: belongs_to :book, I commented it out
You didn't include your AuthorNote model, there might be some validation constraints that prevents the author_note from being saved.
Your code also doesn't handle validation error, so you might want to do that. But you can simply check for errors like:
def create
#author_note = AuthorNote.new(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
#author_note.save
# puts works too
logger.debug "author_note save error: #{#author_note.errors.full_messages.join(' ')}"
end
This is highly likely due to rails 5 making belongs_to association required by default.
What that means is from rails > 5, if you define belongs_to on a model and if the corresponding record is not present. In this case, book_id should be nil in the AuthorNote record. ActiveRecord will error it out and rollback the transaction.
To fix this, instead of commenting out the belongs_to relationship all together, you can make it optional (beacause, removing the relationship might break the system)
class AuthorNote < ApplicationRecord
belongs_to :book, optional: true
end
Use ! along with the save or create that will show the error message Object.save! or Object.create!
def create
#author_note = AuthorNote.new(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
#author_note.save!
end
OR
def create
#author_note = AuthorNote.create!(author: params[:author_note][:author], user_id: current_user.id, comment: params[:author_note][:comment])
end
I have an annoying problem: I want to mark users as deleted, so I created a boolean db column named "deleted" and flagged a User as deleted. Now, when I search it:
ds = User.where(deleted: 1)
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted` = 1 LIMIT 11
=> #<ActiveRecord::Relation [#<
User id: 4,
vname: "Max",
nname: "Muster",
...
emailverified: true,
deleted: false>]>
Why does Rails interpret 1 as false? One is supposed to be true, as the following search shows:
ds = User.where(deleted: true)
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted` = 1 LIMIT 11
=> #<ActiveRecord::Relation [#<
User id: 4,
vname: "Max",
nname: "Muster",
...
emailverified: true,
deleted: false>]>
So I search for deleted = true and get a record where deleted = false ???
I need this for a test I am writing:
expect(user_after_deletion.deleted).to be true
But user.deleted is always false, even when 1 is stored in the db.
With the field "emailverified" everything works as expected. The columns are both tinyint(1).
I tried to rename the column, but still no luck.
Can anyone please point me to the right direction?
Thanks!
P.S.:
user.rb
class User < ApplicationRecord
attr_accessor :passwd_upd
belongs_to :entitlement
after_initialize :set_uid
has_secure_password
has_many :user_histories
def self.automatic_deletion
User.where("created_at > ?", (Time.now - 24.hours)).each do |u|
u.deleted = true
u.save(validate: false)
end
end
def undelete
self.deleted = false
end
end
and the corresponding migration.
class AddDeletedToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :deleted, :boolean
end
end
I "solved" it by updating Rails from 5.1.4 to 5.2.
Now I get:
ds = User.where(deleted: true)
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted` = 1 LIMIT 11
=> #<ActiveRecord::Relation [#<
User id: 4,
vname: "Max",
nname: "Muster",
...
emailverified: true,
deleted: true>]>
Sorry for stealing Your time.
I've got two tables, User and Allergy. These are connected via another table, UserAllergy. The models are as would be expected:
class User
has_many :user_allergies
has_many :allergies, through: :user_allergies
end
class UserAllergy
belongs_to :user
belongs_to :allergy
end
class Allergy
has_many :user_allergies
has_many :users, through :user_allergies
end
What I'm confused about is creating allergies from a multiple-valued collection_select in my User form.
I have the following field:
<%= f.collection_select :allergy_ids,
Allergy.all,
:id,
:name,
{},
{ class: 'form-control', multiple: true }
%>
This correctly inserts a key into my params like so if I selected the Allergies with ids 1 and 2:
{ user: { id: "1", allergy_ids: ["", "1", "2"] } }
When I create the user instantiated with #user = User.new( my_params ), the weird behavior occurs. Instead of inserting the provided allergy_ids into the join table, Rails does a query to get all current user_allergies for the user, then deletes all of the current user_allergies:
Started PATCH "/employees/regular_user" for 127.0.0.1 at 2015-06-18 22:08:30 -0400
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "user"=>{ "allergy_ids"=>["", "1", "2", "3"]}, "button"=>"", "id"=>"regular_user"}
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
(0.1ms) begin transaction
Allergy Load (0.1ms) SELECT "allergies".* FROM "allergies" INNER JOIN "user_allergies" ON "allergies"."id" = "user_allergies"."allergy_id" WHERE "user_allergies"."user_id" = ? [["user_id", 1]]
SQL (0.1ms) DELETE FROM "user_allergies" WHERE "user_allergies"."user_id" = ? AND "user_allergies"."allergy_id" = 1 [["user_id", 1]]
(27.4ms) commit transaction
Redirected to http://localhost:3000/employees/regular_user
Completed 302 Found in 32ms (ActiveRecord: 27.8ms)
Anyone knows what gives, or what I need to do to create allergies implicitly? I've tried accepts_nested_attributes_for and changing around the form to use fields_for.
So, I went and looked at code of mine that does a similar function. Here's what my create method looks like. This is creating a Student with assignment to Student Groups in a school setting (I didn't use "class" since Ruby wouldn't like that).
def create
#student = Student.new(student_params)
if #student.save
#student.student_groups = StudentGroup.where(id: params[:student][:student_group_ids])
flash[:success] = "Student was successfully created."
redirect_to #student
else
render 'new', notice: "Your student could not be created."
end
end
I completely ignore the Student Group IDs when creating the student_params, since I'm not using them for mass assignment.
Yes, one extra line of code. I'd be really interested to hear if there's a way to accomplish this via mass assignment.
You're missing one part of the puzzle which is the relation from Allergy to User.
class Allergy
has_many :user_allergies
has_many :users, through: :user_allergies
end
Just give the following code a try-
params.require(:user).permit(___, ____, {allergy_ids: []}, ____, ____)
I have custom attribute setter in a Rails model in which i'm adding validation errors. However when record attributes are being updated 'true' returns as result, which kinda confusing to me. Any hints how to use validation errors inside custom setter?
Model:
class Post < ActiveRecord::Base
attr_accessible :body, :hidden_attribute, :title
def hidden_attribute=(value)
self.errors.add(:base, "not accepted")
self.errors.add(:hidden_attribute, "not_accepted")
write_attribute :hidden_attribute, value unless errors.any?
end
end
Console Output:
1.9.3p194 :024 > Post.last
Post Load (0.2ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
=> #<Post id: 1, title: "asdsaD", body: "la", hidden_attribute: nil, created_at: "2013-11-13 16:55:44", updated_at: "2013-11-13 16:56:06">
1.9.3p194 :025 > Post.last.update_attribute :hidden_attribute, "ka"
Post Load (0.2ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
(0.0ms) begin transaction
(0.0ms) commit transaction
=> true
I've made an example application for this case.
Okay, i understood the core of the issue. It's not possible to do what i want to achieve, because all the validations errors cleared out as soon as validation process starts.
https://github.com/rails/rails/blob/75b985e4e8b3319a4640a8d566d2f3eedce7918e/activemodel/lib/active_model/validations.rb#L178.
Custom setter kicks in way too early :(
In your setters, you can store the error messages in a temporary hash. Then you can create an ActiveRecord validation method to check if this temporary hash is empty and copy the error messages to errors.
For example,
def age=(age)
raise ArgumentError unless age.is_a? Integer
self.age = age
rescue ArgumentError
#setter_errors ||= {}
#setter_errors[:age] ||= []
#setter_errors[:age] << 'invalid input'
end
Here's the ActiveRecord validation
validate :validate_no_setter_errors
def validate_no_setter_errors
#setter_errors.each do |attribute, messages|
messages.each do |message|
errors.add(attribute, message)
end
end
#setter_errors.empty?
end
To see this in action:
[2] pry(main)> p.age = 'old'
=> "old"
[3] pry(main)> p.save!
(1.0ms) BEGIN
(1.2ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Age invalid input
[4] pry(main)> p.errors.details
=> {:age=>[{:error=>"invalid input"}]}
This should work. Notice the change from update_attribute to update_attributes. update_attribute skips validation.
Post.last.update_attributes(:hidden_attribute => "ka")
def hidden_attribute=(value)
self.errors.add(:base, "not accepted")
self.errors.add(:hidden_attribute, "not_accepted")
write_attribute :hidden_attribute, value #removed the condition here because save doesn't do anything when the object is not changed
end
When you don't write the attribute, there is no change on the object and save does nothing but returns true.
course.rb
has_many :current_users, :through => :user_statuses, :source => :user, :conditions => ['user_statuses.updated_at > ?', 1.hour.ago]
console
Loading development environment (Rails 3.2.2)
>> course = Course.find(1)
Course Load (0.3ms) SELECT `courses`.* FROM `courses` WHERE `courses`.`id` = 1 LIMIT 1
=> #<Course id: 1, title: "Course 1", created_at: "2012-04-17 19:17:15", updated_at: "2012-04-17 19:17:15">
>> Time.now
=> 2012-04-23 08:29:45 -0400
>> course.current_users.count
(0.4ms) SELECT COUNT(*) FROM `users` INNER JOIN `user_statuses` ON `users`.`id` = `user_statuses`.`user_id` WHERE `user_statuses`.`user_id` = 1 AND (user_statuses.updated_at > '2012-04-23 12:28:40')
=> 0
>> Time.now
=> 2012-04-23 08:30:07 -0400
>> course.current_users.count
(0.4ms) SELECT COUNT(*) FROM `users` INNER JOIN `user_statuses` ON `users`.`id` = `user_statuses`.`user_id` WHERE `user_statuses`.`user_id` = 1 AND (user_statuses.updated_at > '2012-04-23 12:28:40')
=> 0
>>
Notice when checking the 1.hour.ago condition it uses the same time as a starting point despite the 30 second difference between the times when I made the request. Exiting console and restarting it clears it out, but it happens again with a new time. This behavior exists in testing and a browser as well. How do I get a model to use a time based condition for a has_many :through find?
I believe you want to use a dynamic condition on your models relation.
Have a look at this SO question
Basically when your model loads, 1.hour.ago is evaluated only once. If I understand your question, you want it to be evaluated on each request.
Something like this (rails 3.1+) :
:conditions => lambda { |course| "user_statuses.updated_at > '#{1.hour.ago}'" }
Putting the query in the model at all didn't work, either in a has_many :through setup or in a method. So I ended up removing the association and putting the query in the controller. This allows the current time to be calculated when the request is made.
model:
has_many :user_statuses
controller:
#course = Course.find(params[:id])
#current_users = #course.user_statuses.where('updated_at > ?', 1.hour.ago)