Rails Remove child association from parent
How about this: @contract.accessories.delete(@accessory) See also: How do I remove a single HABTM associated item without deleting the item itself?
How about this: @contract.accessories.delete(@accessory) See also: How do I remove a single HABTM associated item without deleting the item itself?
In Rails 4 you can use User.where.not(id: []) which will give you the correct result. It produces: SELECT “users”.* FROM “users” WHERE (1 = 1) Unfortunately User.where(‘id NOT IN (?)’, []) should be equivalent but it is not. It still gives you the wrong result: SELECT “users”.* FROM “users” WHERE (id NOT IN (NULL)) References: … Read more
Use delegate in the model class. class Team < ActiveRecord::Base belongs_to :division has_many :players delegate :league, to: :division end Reference: http://api.rubyonrails.org/classes/Module.html#method-i-delegate
You’re very close. Concatenating the arrays is done with the plus sign: materials = books + articles Sorting the combined array can be done by calling the sort_by method (mixed in from Enumerable) and passing in the attribute prefixed with &: materials.sort_by(&:created_at) This won’t be good performance-wise for large result sets. You might consider deriving … Read more
Codeigniter active record has a function insert_batch i think that is what you need $data = array( array( ‘title’ => ‘My title’ , ‘name’ => ‘My Name’ , ‘date’ => ‘My date’ ), array( ‘title’ => ‘Another title’ , ‘name’ => ‘Another Name’ , ‘date’ => ‘Another date’ ) ); $this->db->insert_batch(‘mytable’, $data); // Produces: INSERT … Read more
vee’s answer is good, but I have one caveat. select instantiates a User for every row in the result, but pluck does not. That doesn’t matter if you are only returning a few objects, but if you are returning large batches (50, 100, etc) you’ll pay a significant performance penalty. I ran into this problem, … Read more
@parent.children.order(“value DESC”).first
Quite late to the party here, but I ran out of database connections today in production. Like a lot of people, I use Sidekiq to perform asynchronous jobs like sending emails for example. It is important to note that Sidekiq runs as a multithread process. So, I don’t just have a single-threaded Rails application, therefore … Read more
The answer may be different regarding what you want to achieve. Do you want to retrieve those attributes or to use them for querying ? Loading results ActiveRecord is about mapping table rows to objects, so you can’t have attributes from one object into an other. Let use a more concrete example : There are … Read more
I had to dig into the Rails (3.0.7) code to find some differences. The core functionality looks the same to me — they both seem to call valid? on the associated record(s). The key differences that I did find only appear when using the :autosave feature or when either destroying the associated object or marking … Read more