activerecord

Order products by association count

会有一股神秘感。 提交于 2019-12-28 12:43:10
问题 Class Product has_many :sales end Class Sale belongs_to :product end How do i get the most sold products.. (Product find all.. order by .. ventas..) ? 回答1: When retrieving the Products, you could do: @products = Product.find(:all, :include => :sales, :order => "sales.value DESC") 'sales.value' can be replaced with whatever value you are trying to order by... EDIT: Disregard the rest of my answer. it won't return the Product objects in descending order by sale value, it'll return the Sales

How do I avoid multiple queries with :include in Rails?

淺唱寂寞╮ 提交于 2019-12-28 11:54:59
问题 If I do this post = Post.find_by_id(post_id, :include => :comments) two queries are performed (one for post data and and another for the post's comments). Then when I do post.comments, another query is not performed because data is already cached. Is there a way to do just one query and still access the comments via post.comments? 回答1: No, there is not. This is the intended behavior of :include , since the JOIN approach ultimately comes out to be inefficient. For example, consider the

Rails create or update magic?

…衆ロ難τιáo~ 提交于 2019-12-28 07:35:09
问题 I have a class called CachedObject that stores generic serialised objects indexed by key. I want this class to implement a create_or_update method. If an object is found it will update it, otherwise it will create a new one. Is there a way to do this in Rails or do I have to write my own method? 回答1: Not if you are looking for an "upsert" (where the database executes an update or an insert statement in the same operation) type of statement. Out of the box, Rails and ActiveRecord have no such

Return a grouped list with occurrences using Rails and PostgreSQL

雨燕双飞 提交于 2019-12-28 07:15:09
问题 I have a list of Tags in my rails application which are linked to Posts using Taggings . In some views, I'd like to show a list of the 5 most commonly used tags, together with the times they have been tagged. To create a complete example, assume a table with 3 posts: POSTS ID | title 1 | Lorem 2 | Ipsum 3 | Dolor And a table with 2 Tags TAGS ID | name 1 | Tag1 2 | Tag2 Now, if Post 1 is tagged with Tag1 and post 2 is tagged with tags 1 and 2, our taggings table looks like this: TAGGINGS tag

Pass array to where in Codeigniter Active Record

你离开我真会死。 提交于 2019-12-28 05:56:33
问题 I have a table in my database with adminId and clientId There might be 20 records with the adminId of the logged in user and I'm trying to pull a list of clients. I am wondering if there is a way i can say something like: $this->db->where('id', '20 || 15 || 22 || 46 || 86'); I'm trying to do this with dynamic data (you never know how many clients Id's you'll need to pull). Any ideas? 回答1: $this->db->where_in('id', ['20','15','22','42','86'); Reference: where_in 回答2: Use where_in() $ids =

ORM/DAO/DataMapper/ActiveRecord/TableGateway differences?

眉间皱痕 提交于 2019-12-28 04:57:04
问题 Can you, please, explain me the differences between the following database representatives, say, in PHP.: ORM DAO DataMapper ActiveRecord TableGateway Any examples would be appreciated. 回答1: That would require a pretty long answer. Instead of repeating what others have said better and in more detail before me, I link you to some relevant pages. I suggest to look through them. Maybe follow a few additional links. Wikipedia is always a good start. If you still have any questions about one or

Is there a way to avoid automatically updating Rails timestamp fields?

点点圈 提交于 2019-12-28 03:31:12
问题 If you have DB columns created_at and updated_at Rails will automatically set those values when you create and update a model object. Is there a way to save the model without touching those columns? I am bringing in some legacy data and I would like to set those values from the corresponding values in the (differently named) legacy data fields. I'm finding when I set them on the model and then save the model, Rails appears to override the incoming values. Of course I could just name the Rails

What is the difference between build and new on Rails?

烂漫一生 提交于 2019-12-28 03:30:50
问题 Can anyone tell me what is the difference between build and new command on Rails? 回答1: new is for a new instance of a specific model: foo = Foo.new build is for creating a new instance within an AR association: bar = foo.build_bar # (has_one or belongs_to) or bar = foo.bars.build # (has\_many, habtm or has_many :through) http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Update Per @toklands's suggestion, build and new are aliases as defined in ActiveRecord:

“WARNING: Can't mass-assign protected attributes”

折月煮酒 提交于 2019-12-28 03:29:05
问题 I have used RESTful techniques to generate a model (in fact, I am using Devise gem, which does that for me), and I have added new fields called first_name and last_name to the model. Migration went fine. I added attr_accessor :first_name, :last_name to the model and expected it would just work. But when I try to mass-assign new instances with Doctor.create({:first_name=>"MyName"}) etc., I am getting errors saying I can't mass-assign protected attributes. I thought the whole point of using

How to test for (ActiveRecord) object equality

孤街醉人 提交于 2019-12-27 23:36:47
问题 In Ruby 1.9.2 on Rails 3.0.3 , I'm attempting to test for object equality between two Friend (class inherits from ActiveRecord::Base ) objects. The objects are equal, but the test fails: Failure/Error: Friend.new(name: 'Bob').should eql(Friend.new(name: 'Bob')) expected #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil> got #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil> (compared using eql?) Just for grins, I also test for object