activerecord

Ruby ActiveRecord getting a range up to last entry?

混江龙づ霸主 提交于 2019-12-24 16:42:55
问题 Suppose I have 100 entries in my database, my mobile application has an infinite listview which queries the database for the entries. The application displays items in batches of 15, once a user reaches the end of the 15 items, the application will query for the next 15 items. This means that there will be a remained of 10 items to make a total 100 items. I have tried looking at some of the ActiveRecord's method such as find : # Find the clients with primary keys 1 and 10. clients = Client

Rails 3 - Find condition to filter broken Reference integrity associations

a 夏天 提交于 2019-12-24 16:41:30
问题 I have two models: TimeLog and Task . TimeLog belongs to Task and Task has many TimeLog s. In the past some Task s were deleted but the corresponding TimeLog s were not deleted (the cascade delete wasn't working). So we have some broken TimeLog s. They do have a task_id but that task_id does not exist anymore. I have two questions: 1) I want to get all the TimeLog s from a user but filtering the broken ones. i.e TimeLog.find(:all, :conditions => ['time_log.user_id = ? and <time_log.task_id

Rails 5: Nested forms and existing associated objects

╄→尐↘猪︶ㄣ 提交于 2019-12-24 16:41:03
问题 I'm relatively new to Rails so please forgive me if this turns out to be quite a naive question. :] I have two models that can be tagged: Collection and Video . I support this via a Tag model with a polymorphic association, and a has_many :through association to Tagging . All this works perfectly. What I'm having a hard time figuring out is how to set up the views to accommodate this. I know that if I were only creating or updating tags that are uniquely associated with a single model then I

How does Rails import existing rows when you “Edit” in scaffolding?

时光毁灭记忆、已成空白 提交于 2019-12-24 16:15:29
问题 That was probably a very badly worded question, but I'm curious how Rails knows to bring in the form data when you click edit. For example, if I scaffolded a model called Post, and typed in a title and content for that Post, later when I edit it, the form is automatically filled in. I looked at the controller and the form partial, but it doesn't seem like it contains anything to tell it to fill it up with the existing data. The reason I am asking this is because I want to allow users to

Activerecord: Pluck specific columns and association's association counts

巧了我就是萌 提交于 2019-12-24 16:13:27
问题 Alright, so we have Accounts which has many Schedules which has many Impressions . I want to get Accounts.name , all of Accounts.schedules , but only plucking Accounts.schedules.date , Accounts.schedules.summary and also the count of Accounts.schedules.impressions Here's a visualization of what I want given 1 account (I'll want all accounts) Account: .name Schedules .date .summary count(Schedule.impressions) And here's what the code would look like with too many queries Account.find_each do

Rails: How to fetch records that are 2 'has_many' levels deep?

帅比萌擦擦* 提交于 2019-12-24 16:07:22
问题 I have these models set up: Course has_and_belongs_to_many Student Student has_and_belongs_to_many Course has_many Books Book belongs_to Student How do I efficiently get all the Books for a Course with ActiveRecord? 回答1: Try this: Course.includes(:students => { :books }) Documentation is here, under "Eager loading of associations". Edited Sorry, I misread the question. I see your focus is on the books for a given course. In that case I would recommend something like this: Book.includes(

Load Ruby on Rails models without loading the entire framework

只愿长相守 提交于 2019-12-24 15:33:47
问题 I'm looking to create a custom daemon that will run various database tasks such as delaying mailings and user notifications (each notice is a separate row in the notifications table). I don't want to use script/runner or rake to do these tasks because it is possible that some of the tasks only require the create of one or two database rows or thousands of rows depending on the task. I don't want the overhead of launching a ruby process or loading the entire rails framework for each operation.

Testing before_create method in rspec and rails 3

别等时光非礼了梦想. 提交于 2019-12-24 15:33:26
问题 I've looked into some tutes and all I saw were old posts on how to test before_create. Also it seems like they're all just testing that before_create was called i.e.: @user = User.new @user.should_receive(:method_name_called_by_before_create) @user.send(:before_create) (sometimes they just do @user.save) I want to actually test that my method worked and that it had assigned(and saved the variables) after creating the record. Here are my models: user.rb class User < ActiveRecord::Base has_one

Get the count of rows count after GROUP BY

眉间皱痕 提交于 2019-12-24 13:34:09
问题 Here is the code I use in a Ruby on Rails project to find residences which have amenities with the ids 48, 49 and 50. They are connected with a has_many through connection. id_list = [48, 49, 50] Residence.joins(:listed_amenities). where(listed_amenities: {amenity_id: id_list}). group('residences.id'). having("count(listed_amenities.*) = ?", id_list.size) The resulting SQL: SELECT "residences".* FROM "residences" INNER JOIN "listed_amenities" ON "listed_amenities"."residence_id" = "residences

Yii: using active record with autocommit off on mysql server

自作多情 提交于 2019-12-24 12:54:50
问题 Just wrote a Yii app and on the development mysql server the autocommit was set to true. Now when the app went into production I just realized that the mysql server autocommit is set to false. The app is using active record to save (which auto commits). Is there a variable that I can set in the config db file instead of having to add a beginTransaction and commit in each write to the db? 回答1: CDbConnection has an autoCommit property. I haven't tested it yet though. EDIT because of PHP MySQL