activerecord

Rails 3: Arel for NOT EXISTS?

橙三吉。 提交于 2019-12-18 15:35:47
问题 How do you write a NOT EXISTS in Arel? I'm having trouble translating this query into Arel: SELECT * FROM deals WHERE NOT EXISTS ( SELECT 1 FROM reward_deals WHERE reward_deals.deal_id = deal.id AND NOT ( reward_deals.awarding_type = 'deal' AND reward_deals.deal_id = reward_deals.awarding_id ) ) 回答1: Here is the answer, with strange names because I don't know how to give names for a domain that is for me unknown. deals = Deal.arel_table reward_deals = RewardDeal.arel_table awarding_condition=

find_by_sql with array format in Rails 3

◇◆丶佛笑我妖孽 提交于 2019-12-18 15:35:36
问题 good day guys! I'm using find_by_sql() in rails 3 to fetch records as follows. @list=Email.find_by_sql(["SELECT * FROM Emails WHERE sent_id=?",params[:id]]) How to modify the same statement if multiple parameter applies for same attribute, say for example : @list=Email.find_by_sql(["SELECT * FROM Emails WHERE (sent_id=? OR from_id=?)",params[:id],params[:id]]) Here, both sent_id and from_id attributes receives same parameter params[:id] So, instead of passing same params[:id] twice, is there

How to convert ActiveRecord table name to model class name

这一生的挚爱 提交于 2019-12-18 14:43:53
问题 Is there any possibility to properly convert ActiveRecord table name to model class name? I have found one hack def model_for_table(table_name) table_name.classify.constantize end but since we use set_table_name for many of our models this wont work. Is there any way to do it? 回答1: I did it! This returns a hash in the form of "table_name" => "model_class_name". Hash[ObjectSpace.enum_for(:each_object, class << ActiveRecord::Base; self; end).to_a.reject{|c| c == ActiveRecord::Base}.collect{ |c|

Active Record - Get the second, third.. item in a database (without ID)

邮差的信 提交于 2019-12-18 14:37:07
问题 How to I retrieve the second, third .. entries in a database. I don't want to use the auto incrementing id generated by rails. As I am deleting entries from my database, so the ID continues to increase. So my DB would have id : 210 (Even thought it's currently the first value) id : 211 (Even thought it's currently the second value) I know "Phone.first" will return the first how do I get the second. Sub Question- Destroy_all/delete_all - only removes the item, Can I remove all entries from the

How can I find a model's relationships?

 ̄綄美尐妖づ 提交于 2019-12-18 14:14:36
问题 I want to, when given a particular model, return all the related models it is associated with. For example: class Dog < ActiveRecord::Base has_many :bones belongs_to :master end d = Dog.first d.associations #<== should return [Bone, Master] Is there a way to do this already without having to roll my own? Failing that, any suggestions for the best way to do this? 回答1: Dog.reflect_on_all_associations http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#M001405 You wouldn

first_or_create vs find_or_create_by

五迷三道 提交于 2019-12-18 14:11:12
问题 first_or_create seems much less documented, so I wondered if it was just because the two methods are synonymous. 回答1: Basically: Foo.where(attributes).first_or_create Is the same as: Foo.find_or_create_by(attributes) #first_or_create is sometimes misunderstood as people expect it to search by the attributes given, but that is not the case. Aka Foo.first_or_create(attributes) Will not search for a foo that satisfies the attributes . It will take the first foo if any are present. It's useful if

first_or_create vs find_or_create_by

我是研究僧i 提交于 2019-12-18 14:10:56
问题 first_or_create seems much less documented, so I wondered if it was just because the two methods are synonymous. 回答1: Basically: Foo.where(attributes).first_or_create Is the same as: Foo.find_or_create_by(attributes) #first_or_create is sometimes misunderstood as people expect it to search by the attributes given, but that is not the case. Aka Foo.first_or_create(attributes) Will not search for a foo that satisfies the attributes . It will take the first foo if any are present. It's useful if

Is it a good idea to purge old Rails migration files?

半腔热情 提交于 2019-12-18 13:55:08
问题 I have been running a big Rails application for over 2 years and, day by day, my ActiveRecord migration folder has been growing up to over 150 files. There are very old models, no longer available in the application, still referenced in the migrations. I was thinking to remove them. What do you think? Do you usually purge old migrations from your codebase? 回答1: They are relatively small, so I would choose to keep them, just for the record. You should write your migrations without referencing

Cleaning up Paperclip error messages

坚强是说给别人听的谎言 提交于 2019-12-18 13:38:27
问题 Okay, so i've got paperclip working, and I'm trying to use the built in validator to make sure that the file uploaded Is an image Is not too big So I have this in the model, per the documentation: validates_attachment :avatar, :content_type => { :content_type => /image/ }, :size => { :in => 0..2.megabytes } However the error it shows in the view is this mess: I'd like it to be something a bit simpler, like "Avatar must be an image less than 2 megabytes" However, I can't see where to do this,

Is there a way to validate a specific attribute on an ActiveRecord without instantiating an object first?

陌路散爱 提交于 2019-12-18 13:12:55
问题 For example, if I have a user model and I need to validate login only (which can happen when validating a form via ajax), it would be great if I use the same model validations defined in the User model without actually instantiating a User instance. So in the controller I'd be able to write code like User.valid_attribute?(:login, "login value") Is there anyway I can do this? 回答1: Since validations operate on instances (and they use the errors attribute of an instance as a container for error