activerecord

Does rails support mysql json data type

核能气质少年 提交于 2019-12-12 09:37:05
问题 I'm aware that in rails we can use a text data type for columns in mysql that we want to save hashes or array to it, where rails serialize the hash in yaml format and save it in the column. class A < ActiveRecord::Base serialize :data, Hash end However if I need to perform some search on this column, I have to load all records and de-serialize all the hashes, and use ruby to search within the hashes. So is there a way to tell mysql to search within the hashes and return matched records ? I

Rails belongs_to through association

白昼怎懂夜的黑 提交于 2019-12-12 09:28:42
问题 I'm trying to add a new model to an existing model mesh. The existing one works perfectly but I can't get the new one to work properly and am wondering if the association is able to work the way I'm trying to make it work. Update: As I just got asked: belongs_to through was something I've read while gooling about the problem. If it doesn't exist, would has_one through be the correct way? I tried it as well but it also didn't work. Here is the existing mesh: class Course has_many :users,

Ruby on Rails where query with relations

隐身守侯 提交于 2019-12-12 09:22:21
问题 I am trying to use where query with relationships. How can I query using where with relations in this case? This is model User has_many :projects has_many :reasons, through: :projects Project belongs_to :user has_many :reasons Reasons belongs_to :project This is the codes which doesn't work # GET /reasons def index reasons = current_user.reasons updated_at = params[:updated_at] # Filter with updated_at for reloading from mobile app if updated_at.present? # This one doesn't work!!!!!!!!!!!!

Rails - EAV model with multiple value types?

大城市里の小女人 提交于 2019-12-12 09:21:22
问题 I currently have a model Feature that is used by various other models; in this example, it will be used by customer. To keep things flexible, Feature is used to store things such as First Name, Last Name, Name, Date of Birth, Company Registration Number, etc. You will have noticed a problem with this - while most of these are strings, features such as Date of Birth would ideally be stored in a column of type Date (and would be a datepicker rather than a text input in the view). How would this

Rails AR record includes 'new' unsaved record

删除回忆录丶 提交于 2019-12-12 09:18:42
问题 I have a view for an 'article' show action. Articles have comments. The view displays: a) article b) comments c) new comment form My controller is action is: def show @article = Article.find(params[:id]) @comments = @article.comments @comment = @comments.new end The view displays the comments using a partial. <%= render @comments %> When looping through @comments I am finding the list also contains the new record from the controller action. I can avoid displaying this row using: <% if

Throw an exception whenever someone tries to mass-assign protected attributes

Deadly 提交于 2019-12-12 08:36:17
问题 I'm fixing some mass assignment vulnerabilities in a client's application and I want to make sure Rails isn't silently dropping attempts to mass assign protected attributes. Instead, I want to throw an exception so I can investigate. I.e., whenever this would normally appear in the logs: WARNING: Can't mass-assign these protected attributes: ... I'd like to throw an exception instead (or in addition) Edit: I'm using Rails 2.3.4 回答1: You'll have to do some Rails monkey-patching to do this. Be

Yii - Get min, max of a column using active record

廉价感情. 提交于 2019-12-12 08:35:19
问题 I am using active record. Lets call the model Product. How can i get "select min(price) from tbl_product where name like '%hair spray%'" using active record? 回答1: You could use something like this: $criteria = new CDbCriteria; $criteria->select='MIN(price) as minprice'; $criteria->condition='name LIKE :searchTxt'; $criteria->params=array(':searchTxt'=>'%hair spray%'); $product = Product::model()->find($criteria); You would just need to declare: public $minprice; in your model class. (Using

Using the database features of the Yii framework

吃可爱长大的小学妹 提交于 2019-12-12 08:16:53
问题 I recently started working with Yii PHP MVC Framework. I'm looking for advice on how should I continue working with the database through the framework: should I use framework's base class CActiveRecord which deals with the DB, or should I go with the classic SQL query functions (in my case mssql)? Obviously or not, for me it seems easier to deal with the DB through classic SQL queries, but, at some point, I imagine there has to be an advantage in using framework's way. Some SQL queries will

CoffeeScript to select form fields dynamically on change and on load

陌路散爱 提交于 2019-12-12 08:12:31
问题 I have a Rails app where I'm trying to select a list of facilities based on what region is selected in a form. So far, I've implemented group_collection_select to do this as well as a bit of CoffeeScript. It works when creating a new record and selecting a region. The behavior being to only show facilities listed for the selected region. What does not work is when editing a record, selecting the facilities shows all of the facilities grouped by region instead of constraining the facilities to

How to (massively) reduce the number of SQL queries in Rails app?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:05:35
问题 In my Rails app I have users which can have many invoices which in turn can have many payments . Now in the dashboard view I want to summarize all the payments a user has ever received, ordered either by year, quarter, or month. The payments are also subdivided into gross , net , and tax . user.rb : class User < ActiveRecord::Base has_many :invoices has_many :payments def years (first_year..current_year).to_a.reverse end def year_ranges years.map { |y| Date.new(y,1,1)..Date.new(y,-1,-1) } end