activerecord

Rails 3 ActiveRecord eager loading of scope

╄→尐↘猪︶ㄣ 提交于 2019-12-18 02:28:08
问题 Help me please. I have some model which has_many association with other model. For example: profile => has_many :statistics And inside of statistic model I have some scope: scope last_ten, limit(10).order('online desc') And question is how can I use eager load for this scope? I don't need every record of statistics for profile. Only scoped. Now I can use only User.profiles.includes(:statistics) Thanks. 回答1: As explained here: http://api.rubyonrails.org/classes/ActiveRecord/Associations

ActiveRecord select except columns

喜欢而已 提交于 2019-12-18 02:12:28
问题 Is there a way I can specify to select ALL columns in ActiveRecord except just a few. For example, for a User, I don't want to select their password hash or their email. Is this possible or do I have to manually hardcode all columns? Thanks 回答1: write a scope like def select_without columns select(column_names - columns.map(&:to_s)) end 回答2: Something like this? exclude_columns = ['password', 'email'] columns = User.attribute_names.delete_if(|x| exclude_columns.include?(x)) User.select

Rails: Exception in after_create stopping save

倖福魔咒の 提交于 2019-12-18 02:01:29
问题 Simple question. I have a ActiveRecord model that I want to perform post processing on AFTER the record is saved. So in the model I have a queue_for_processing method that sticks a job onto my Resque queue. To make this execute after my record is successfully persisted I have written the following in my model: after_create :queue_for_processing Pretty simple. I had thought that everything was working as expected EXCEPT that last night my redis server went down and things went awry. My

Rails: Exception in after_create stopping save

旧巷老猫 提交于 2019-12-18 02:01:07
问题 Simple question. I have a ActiveRecord model that I want to perform post processing on AFTER the record is saved. So in the model I have a queue_for_processing method that sticks a job onto my Resque queue. To make this execute after my record is successfully persisted I have written the following in my model: after_create :queue_for_processing Pretty simple. I had thought that everything was working as expected EXCEPT that last night my redis server went down and things went awry. My

Why alias_method fails in Rails model

不问归期 提交于 2019-12-18 01:52:21
问题 class Country < ActiveRecord::Base #alias_method :name, :langEN # here fails #alias_method :name=, :langEN= #attr_accessible :name def name; langEN end # here works end In first call alias_method fails with: NameError: undefined method `langEN' for class `Country' I mean it fails when I do for example Country.first . But in console I can call Country.first.langEN successfully, and see that second call also works. What am I missing? 回答1: ActiveRecord uses method_missing (AFAIK via ActiveModel:

Ruby on Rails: How to join two tables

北城余情 提交于 2019-12-18 01:13:09
问题 I have an index page that I want to show all the users' profile and their associated photos. I'm using the plugin Paperclip for the photos. In the Profiles controller, I have the instance variable @profile but it shows me the table in the profiles table only and not the photos table. @profile = Profile.find(:all, :include => :photos, :joins => "INNER JOIN photos ON photos.profile_id = profiles.id") The models are shown below: class Profile < ActiveRecord::Base has_many :photos end class Photo

How do I get Rails to eager load counts?

三世轮回 提交于 2019-12-17 23:46:58
问题 This is related to a question a year and change ago. I put up an example of the question that should work out of the box, provided you have sqlite3 available: https://github.com/cairo140/rails-eager-loading-counts-demo Installation instructions (for the main branch) git clone git://github.com/cairo140/rails-eager-loading-counts-demo.git cd rails-eager-loading-counts-demo rails s I have a fuller write-up in the repository, but my general question is this. How can I make Rails eager load counts

CodeIgniter Active Record - Get number of returned rows

情到浓时终转凉″ 提交于 2019-12-17 22:30:36
问题 I'm very new to CodeIgniter and Active Record in particular, I know how to do this well in normal SQL but I'm trying to learn. How can I select some data from one of my tables, and then count how many rows are returned using CodeIgniters Active Record class? Thanks, Tom. 回答1: Have a look at the result functions here: $this->db->from('yourtable'); [... more active record code ...] $query = $this->db->get(); $rowcount = $query->num_rows(); 回答2: AND, if you just want to get a count of all the

Yii2 How to perform where AND or OR condition grouping?

喜欢而已 提交于 2019-12-17 22:18:37
问题 I am new to Yii-2 framework. How can i achieve following query in Yii-2 framework using activeQuery and models. SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1) Thanks 回答1: You can try this: //SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1) $model = arname()->find() ->andWhere(['user_id'=>[1,5,8]]) ->andWhere(['or',

Rails model that has both 'has_one' and 'has_many' but with some constraints

陌路散爱 提交于 2019-12-17 21:56:58
问题 I am mapping 2 models: User Account class Account has_many :users class User has_one :account The user table as the account_id in it. Now on the Account model I want to create a 'primary user' which an account only has 1 off. The user table has a boolean flag :is_primary, how can I create a has_one on the account side for a user who has the is_primary and account_id mapped. So the SQL would look like: SELECT * FROM users where account_id=123 and is_primary = 1 So I want: A user has an account