rails-activerecord

How do I get the name of a Ruby class?

半腔热情 提交于 2019-12-17 10:07:54
问题 How can I get the class name from an ActiveRecord object? I have: result = User.find(1) I tried: result.class # => User(id: integer, name: string ...) result.to_s # => #<User:0x3d07cdc>" I need only the class name, in a string ( User in this case). Is there a method for that? I know this is pretty basic, but I searched both Rails' and Ruby's docs, and I couldn't find it. 回答1: You want to call .name on the object's class: result.class.name 回答2: Here's the correct answer, extracted from

Rails update_attributes without save?

感情迁移 提交于 2019-12-17 07:59:16
问题 Is there an alternative to update_attributes that does not save the record? So I could do something like: @car = Car.new(:make => 'GMC') #other processing @car.update_attributes(:model => 'Sierra', :year => "2012", :looks => "Super Sexy, wanna make love to it") #other processing @car.save BTW, I know I can @car.model = 'Sierra' , but I want to update them all on one line. 回答1: I believe what you are looking for is assign_attributes. It's basically the same as update_attributes but it doesn't

LEFT OUTER JOIN in Rails 4

北慕城南 提交于 2019-12-17 06:06:39
问题 I have 3 models: class Student < ActiveRecord::Base has_many :student_enrollments, dependent: :destroy has_many :courses, through: :student_enrollments end class Course < ActiveRecord::Base has_many :student_enrollments, dependent: :destroy has_many :students, through: :student_enrollments end class StudentEnrollment < ActiveRecord::Base belongs_to :student belongs_to :course end I wish to query for a list of courses in the Courses table, that do not exist in the StudentEnrollments table that

query , can not select column count

こ雲淡風輕ζ 提交于 2019-12-17 05:14:22
问题 Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name') Build query: SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags` INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id` GROUP BY quote_tags.tag_id ORDER BY count desc Result: [#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">] It not return count column for me. How can I get it? 回答1: Have you tried calling the count method on one of

Rails 5: ActiveRecord OR query

人盡茶涼 提交于 2019-12-17 03:34:25
问题 How do you do an or query in Rails 5 ActiveRecord? Also, is it possible to chain or with where in ActiveRecord queries? 回答1: The ability to chain or clause along with where clause in ActiveRecord query will be available in Rails 5 . See the related discussion and the pull request. So, you will be able to do the following things in Rails 5 : To get a post with id 1 or 2: Post.where('id = 1').or(Post.where('id = 2')) Some other examples: (A && B) || C: Post.where(a).where(b).or(Post.where(c))

Random record in ActiveRecord

与世无争的帅哥 提交于 2019-12-17 02:04:16
问题 I'm in need of getting a random record from a table via ActiveRecord. I've followed the example from Jamis Buck from 2006. However, I've also come across another way via a Google search (can't attribute with a link due to new user restrictions): rand_id = rand(Model.count) rand_record = Model.first(:conditions => ["id >= ?", rand_id]) I'm curious how others on here have done it or if anyone knows what way would be more efficient. 回答1: I haven't found an ideal way to do this without at least

Rails 4: List of available datatypes

ぐ巨炮叔叔 提交于 2019-12-17 01:24:12
问题 Where can I find a list of data types that can be used in Ruby on Rails 4? Such as text string integer float date I keep learning about new ones and I'd love to have a list I could easily refer to. 回答1: Here are all the Rails 4 (ActiveRecord migration) datatypes: :binary :boolean :date :datetime :decimal :float :integer :bigint :primary_key :references :string :text :time :timestamp Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add

activerecord-postgres-hstore after save error ordering in Hash

拟墨画扇 提交于 2019-12-14 03:56:59
问题 store as Hash Table with Hstore, wrong ordering in Hash after Save class Service < ActiveRecord::Base serialize :properties, ActiveRecord::Coders::Hstore end service = Service.new service.properties = { "aaa" => 1, "zz" => 2, "cc" => 3, "d" => 4 } #=> { "aaa" => 1, "zz" => 2, "cc" => 3, "d" => 4 } service.save reload! service = Service.find(:id) service.properties #=> { "d" => "4", "cc" => "3", "zz" => 2, "aaa" => 1 } Bug::: wrong ordering after save Is it because after serialize that it

How to find items with *all* matching categories

天大地大妈咪最大 提交于 2019-12-14 02:37:43
问题 I have two models, Item and Category, joined by a join table. I would like to query Item to find only items that match a list of categories. My models look like: class Item < ActiveRecord::Base has_and_belongs_to_many :categories end class Category < ActiveRecord::Base has_and_belongs_to_many :items end I can easily find items that match ANY of the list of categories. The following will return items that belong to category 1, 2 or 3. Item.includes(:categories).where(categories: {id:[1,2,3]})

How to join an indirect association in ActiveRecord query in Ruby on Rails?

这一生的挚爱 提交于 2019-12-14 01:41:09
问题 In my Ruby on Rails application, I have a model Instance which belongs to another model Zone . The Zone model itself belongs to Country model. I am fetching a set of Instance objects as follows: scope :thisweek, -> { joins(:zone).where(zones: {created_at: ...}).includes(:zone) I would like to join Country to Zone and Instance as well, and then sort the result Instance set based on the zone.country.name field. Anyone can help me please? 回答1: You can try the following: scope :this_week, proc do