rails-activerecord

How to test ActiveRecord witout Rails?

放肆的年华 提交于 2019-12-11 09:37:33
问题 I have a project in which I use ActiveRecord to store information in a sqlite db file. I'm not using Rails and AR seems to do the job perfectly. My question is how exactly to test my classes witout hitting the db? I found some gems that would to the trick (FactoryGirl, UnitRecord), but they are meant to work with Rails. class News < ActiveRecord::Base belongs_to :feed def delete_old_news_if_necessary # time_limit = Settings::time_limit return if time_limit.zero? News.destroy_all("date < #

Rails ActiveRecord: how to get non-duplicate child records by super-parent in view?

橙三吉。 提交于 2019-12-11 09:07:04
问题 I have four tables called Albums , Songs , Singers and SongSingers . Albums { Id, Name } Songs { Id, AlbumId, Name } Singers { Id, Name } SongSingers { Id, SongId, SingerId } Album has many songs. Song belongs to an Album. SongSinger belongs to Song and Singer. Song and Singer have many SongSingers. Each song may have same or different singers. In View, how can I get all non-duplicate Singers by AlbumId. Thanks. 回答1: Absolutely no problem. You are querying for singers. That's what the query

How to model a doctor and patient appointments in rails?

流过昼夜 提交于 2019-12-11 08:04:06
问题 Rails and stackoverflow beginner here so please pardon me for any mistakes. I am developing an appointment booking system between doctors and patients. Now, the flow goes like this A doctor will create its available time slots and the patients can see the available time slots of a particular doctor and can book the time slot. class Doctor < ApplicationRecord has_many :availabilities has_many :appointments has_many :patients, through: :appointments end class Availibilities < ApplicationRecord

How to call an array of scopes on an ActiveRecord object?

[亡魂溺海] 提交于 2019-12-11 07:39:40
问题 I have an Article model which has recent , compiled , featured scopes. I can chain these scopes like this: Article.recent.compiled.featured Now I have an array of these scopes: scopes = [:recent, :compiled, :featured] or scopes = [:recent, :compiled] I don't know beforehand how many scopes are there in the scopes variable. I just know it's an array of scopes. How can I use this array as a chain of scopes to be called on the Article model as mentioned above? 回答1: just use inject: scopes.inject

Is storing the data as json in mysql a good idea?

淺唱寂寞╮ 提交于 2019-12-11 06:16:16
问题 I got a scenario to have relational tables like a "section has many elements", so I thought of going for has_many relation model like a section has many elements. I got some suggestions from our client saying to save the data as JSON inside a column instead of multiple tables. I am bit confused which approach I should go with. Suggestions are welcome. This is the model am proposing eg : section has_many elements Table : sections id type 1 page Table : elements id type content section_id 1

How to intentionally insert strings with the wrong encoding in Mysql?

心已入冬 提交于 2019-12-11 03:06:43
问题 This is probably one of the weirdest questions that i had to ask in Stackoverflow :) I have a legacy, non-tested PHP application which i can't touch at all. This application uses Mysql and one database per account. So we have thousands of databases. Due to an error that was there way before i started working on it, this app connects with the wrong encoding in Mysql. So in database, where we were supposed to have "é" we actually get "é". Although in application ( due to the wrong connection

SQLite3::ConstraintException: constraint failed:

末鹿安然 提交于 2019-12-11 02:37:47
问题 I'm trying to learn Ruby on Rails and have encountered a problem I need help with. What I'm trying to do is to create a "project manager" with a form, but whne I'm filled in the form and clicks submit the following error is shown: ActiveRecord::StatementInvalid in ProjectsController#create SQLite3::ConstraintException: constraint failed: INSERT INTO "projects" ("created_at", "description", "end_date", "start_date", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?) Rails.root:

Rails: Calling .limit(5) changes order of results

被刻印的时光 ゝ 提交于 2019-12-11 02:15:00
问题 I have a search function that basically runs an ordered list of model records. The problem is whenever I called .search.limit(5) , the results are in a different order from when I call .search Here is some of my method def self.search(server_name, pvp_type) if server_name.nil? result = Rom::Leaderboard.order('pvp_vs desc, win_percent desc').limit(200) end end When I call Rom::Leaderboard.search(nil, 2).pluck(:actor_name) SQL Translation: SELECT "rom_leaderboards"."actor_name" FROM "rom

In Rails, can I order a query by a delegate method?

梦想的初衷 提交于 2019-12-10 21:26:45
问题 I'm having difficulty ordering a query by a delegate method. I've been tasked with helping upgrade a fairly large Rails 3 application to Rails 4. I've come across this query in an index action. I am aware the naming of these objects is horrible and confusing. # measurements_controller.rb def index @measurements = Measurement.includes(:identifier).order(:name) end In Rails 4, I'm getting this error: ERROR: column info_items.name does not exist LINE 1: ...D (info_item_identifiers.name LIKE '%')

How to get earliest and latest dates for associated objects

不想你离开。 提交于 2019-12-10 21:12:08
问题 I am developing an Rails 4 app and I got two models. One is called Project and the other is called Task . Project has many Tasks . Task belongs to Project . The task objects have starts_at and ends_at attributes (datetime). How can I get the earliest starts_at and latest ends_at of all the associated tasks that belongs to a specific project? I tried @project.tasks.max(:starts_at) but it did not work (got error). 回答1: Use maximum instead of max . max is for array maximum: [1,2].max == 2 http:/