rails-activerecord

Rails ActiveRecord: validate single attribute

孤者浪人 提交于 2019-12-20 16:21:43
问题 If there is a way I can validate single attribute in Rails? Something like: ac_object.valid?(attribute_name) I'm gonna use it for AJAX validation of particular model fields. Moving these validations to the Javascript makes code really ugly. 回答1: You can implement your own method in your model. Something like this def valid_attribute?(attribute_name) self.valid? self.errors[attribute_name].blank? end Or add it to ActiveRecord::Base 回答2: Sometimes there are validations that are quite expensive

Rails ActiveRecord: validate single attribute

坚强是说给别人听的谎言 提交于 2019-12-20 16:21:21
问题 If there is a way I can validate single attribute in Rails? Something like: ac_object.valid?(attribute_name) I'm gonna use it for AJAX validation of particular model fields. Moving these validations to the Javascript makes code really ugly. 回答1: You can implement your own method in your model. Something like this def valid_attribute?(attribute_name) self.valid? self.errors[attribute_name].blank? end Or add it to ActiveRecord::Base 回答2: Sometimes there are validations that are quite expensive

Possible to specify unique index with NULLs allowed in Rails/ActiveRecord?

拟墨画扇 提交于 2019-12-20 16:19:52
问题 I want to specify a unique index on a column, but I also need to allow NULL values (multiple records can have NULL values). When testing with PostgreSQL, I see that I can have 1 record with a NULL value, but the next will cause an issue: irb(main):001:0> u=User.find(5) User Load (111.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 5]] => #<User id: 5, email: "a@b.com", created_at: "2013-08-28 09:55:28", updated_at: "2013-08-28 09:55:28"> irb(main):002:0> u.email=nil

Dynamically generate scopes in rails models

若如初见. 提交于 2019-12-20 12:19:40
问题 I'd like to generate scopes dynamically. Let's say I have the following model: class Product < ActiveRecord::Base POSSIBLE_SIZES = [:small, :medium, :large] scope :small, where(size: :small) scope :medium, where(size: :medium) scope :large, where(size: :large) end Can we replace the scope calls with something based on the POSSIBLE_SIZES constant? I think I'm violating DRY to repeat them. 回答1: you could do class Product < ActiveRecord::Base [:small, :medium, :large].each do |s| scope s, where

Rails 3.1: can't write to column in same migration that adds it

≯℡__Kan透↙ 提交于 2019-12-20 10:26:43
问题 I had an add_column migration that would run fine. However, after running it and firing up a console, I would find the first_name and last_name columns completely empty. I tried using save! instead and it had the same effect--no errors reported. Here's the original: class UserAddFirstNameAndLastName < ActiveRecord::Migration def change # add column first name, last name string add_column :users, :first_name, :string add_column :users, :last_name, :string User.all.each do |u| u.first_name =

What is Ruby on Rails ORM in layman's terms? Please explain

99封情书 提交于 2019-12-20 08:52:58
问题 I am having trouble understanding ORM in Ruby on Rails. From what I understand there is a 1:1 relationship between tables/columns and objects/attributes. So every record is an object. Also what exactly is a Model? I know it maps to a table. What I'm really after is a deeper understanding of the above. Thank you in advance for your help I'm a Web developer going from PHP to Ruby on Rails. 回答1: "From what I understand there is a 1:1 relationship between tables/columns and objects/attributes. So

Check if a table exists in Rails

梦想的初衷 提交于 2019-12-20 08:03:06
问题 I have a rake task that won't work unless a table exists. I'm working with more than 20 engineers on a website so I want to make sure they have migrated the table before they can do a rake task which will populate that respective table. Does AR have a method such as Table.exists ? How can I make sure they have migrated the table successfully? 回答1: In Rails 5 the API became explicit regarding tables/views, collectively data sources . # Tables and views ActiveRecord::Base.connection.data

Rails can't write to serialized array field in database

时光总嘲笑我的痴心妄想 提交于 2019-12-20 07:14:05
问题 I have the array on my model and set up, and when I go to write the array onto my record it says COMMIT true, yet checking the field on that record immediately after returns an empty array. Model: class Feature < ActiveRecord::Base serialize :content, Array attr_accessible :content end Migration: class AddContentToFeatures < ActiveRecord::Migration def change add_column :features, :content, :text, array: true, default: [] end end What I tried, along with various symbol and string syntaxes, is

How to display highest rated albums through a has_many reviews relationship

霸气de小男生 提交于 2019-12-20 06:36:10
问题 I'm setting up a simple ruby/rails app where users can review albums. On an album's show page I average all the user reviews associated with that album through this code in my albums controller def show @album = Album.find_by_id(params[:id]) if @album.reviews.present? @ratings = Review.where(album_id: @album).average(:rating).truncate(2) else render 'show' end end This gives me an average rating for each album. On my home page (routed through a different controller) I want to display the top

Ruby: how to remove items from array A if it's not in array B?

大憨熊 提交于 2019-12-20 04:37:34
问题 I have prepare these two arrays: list_of_students = Student.where('class = ?', param[:given_class]) list_of_teachers = Teacher.where(...) Student belongs_to Teacher and Teacher has_many students . And now, I'd need to remove from the list_of_students all items (students), where teacher_id is not included in list_of_teachers . I found some tips and HOWTO's on comparing arrays, but none of that helped to figure out this case. Thank you in advance 回答1: You can use the IN SQL statement. list_of