activerecord

Skipping :touch associations when saving an ActiveRecord object

夙愿已清 提交于 2019-12-21 12:10:26
问题 Is there a way to skip updating associations with a :touch association when saving? Setup: class School < ActiveRecord::Base has_many :students end class Student < ActiveRecord::Base belongs_to :school, touch: true end I would like to be able to do something like the following where the touch is skipped. @school = School.create @student = Student.create(school_id: @school.id) @student.name = "Trevor" @student.save # Can I do this without touching the @school record? Can you do this? Something

ActiveRecord has_many :through duplicating counter caches on mass assignment

情到浓时终转凉″ 提交于 2019-12-21 12:00:51
问题 It seems ActiveRecord's counter_cache feature can result in a counter cache being incremented twice. The scenario in which I am seeing this behavior is when I have two models that have a has_many :through relationship with each other through a join model (ie: Teacher has many Student through Classroom ). When using the has_many :through generated methods to associate Teacher and Student directly (without manually creating the join record) the count goes up 2 times. Example: teacher.students <

Codeigniter Database Insert Failure

有些话、适合烂在心里 提交于 2019-12-21 09:35:03
问题 Currently in my controller, when adding new data, I validate the inputs, and if there are any problems it lets a user known, otherwise it passes the data to the model to insert to the database How do I now go about checking the insert statement worked correctly in the model, and let a user known if it did not. Does an insert statement like below, return true or false, which can then be returned to the controller? $this->db->insert('Faviroute_Addresses', $address_data); Thanks for any help 回答1

Codeigniter Allowed memory size exhausted while processing large files

匆匆过客 提交于 2019-12-21 09:29:16
问题 I'm posting this in case someone else is looking for the same solution, seeing as I just wasted two days on this bullshit. I have a cron job that updates the database using a very large file once a day, using the following code: if (($handle = fopen(dirname(__FILE__) . '/uncompressed', "r")) !== FALSE) { while (($data = fgets($handle)) !== FALSE) { $thisline = json_decode($data, true); $this->regen($thisline); } fclose($handle); } This is in a Codeigniter controller that's only used for cron

Rails meta_search gem: sort by count of an associated model

僤鯓⒐⒋嵵緔 提交于 2019-12-21 09:00:43
问题 I'm using meta_search to sort columns in a table. One of my table columns is a count of the associated records for a particular model. Basically it's this: class Shop < ActiveRecord::Base has_many :inventory_records def current_inventory_count inventory_records.where(:current => true).count end end class InventoryRecord < ActiveRecord::Base belongs_to :shop #has a "current" boolean on this which I want to filter by as well end In my Shop#index view I have a table that lists out the current

Rails meta_search gem: sort by count of an associated model

夙愿已清 提交于 2019-12-21 09:00:29
问题 I'm using meta_search to sort columns in a table. One of my table columns is a count of the associated records for a particular model. Basically it's this: class Shop < ActiveRecord::Base has_many :inventory_records def current_inventory_count inventory_records.where(:current => true).count end end class InventoryRecord < ActiveRecord::Base belongs_to :shop #has a "current" boolean on this which I want to filter by as well end In my Shop#index view I have a table that lists out the current

Tool to convert SQL syntax to ActiveRecord query

…衆ロ難τιáo~ 提交于 2019-12-21 08:18:12
问题 There are many questions to help converting specific SQL query to ActiveRecord query. Are there some assistance/guidance [online] tools to make the conversion automatically? 回答1: here is the tool you are looking: http://www.scuttle.io/ 来源: https://stackoverflow.com/questions/23125261/tool-to-convert-sql-syntax-to-activerecord-query

What is the best way to override Rails ActiveRecord destroy behavior?

*爱你&永不变心* 提交于 2019-12-21 08:18:07
问题 I have an application where I would like to override the behavior of destroy for many of my models. The use case is that users may have a legitimate need to delete a particular record, but actually deleting the row from the database would destroy referential integrity that affects other related models. For example, a user of the system may want to delete a customer with whom they no longer do business, but transactions with that customer need to be maintained. It seems I have at least two

Validate uniqueness of many to many association in Rails

荒凉一梦 提交于 2019-12-21 07:59:14
问题 Say I have Project , that is in many-to-many association with Tag . I'm using has_many through so I have separate join model. How do I create validation, that checks uniqueness of join model? Now I have only has_many :tags, :through => :taggings, :uniq => true But that doesn't validate on save. 回答1: Try validates_associated. That should, I believe, allow the join model validations to run before saving. So in your case: class Project has many :tags, :through => :taggings validates_associated

How best to associate an Address to multiple models in rails?

安稳与你 提交于 2019-12-21 07:51:49
问题 This question on SO appears to be related to my question, but I'm not sure my question is answered by that. An address can belong to more than one model (UserProfile and Event) What's the correct way to implement this? The basic tables: user_profiles(id) events(id) Options for implementing the addresses table: addresses(id,user_profile_id,event_id) This approach seems to be kludgy since if tomorrow the address needs to belong to one more model, I have to add that id field. Also, I don't know