activerecord

Rails includes nested relations

巧了我就是萌 提交于 2019-12-21 04:09:08
问题 I need to query all posts from a specific user and include all comments and the user who belongs to the comment. class User < ... has_many :posts has_many :comments end class Post < ... belongs_to :user has_many :comments end class Comment < ... belongs_to :user belongs_to :post end @posts = current_user.posts.include(:comments) Is is possible to also get the comment user? I list a lot of posts and comments. I do not want to query each comment user. Thx / Tobias 回答1: Try @posts = current_user

How can I query Rails ActiveRecord data stored in arrays

戏子无情 提交于 2019-12-21 03:56:09
问题 I have a rails model call MentorData and it has an attribute called os_usage . The oses are stored in an array like so ['apple', 'linux'] . To recap: $ MentorData.first.os_usage => ['apple', 'linux'] I am looking to be able to query the data for all MentorData that includes the os_usage of apple , but when I search MentorData.where(os_usage: 'apple') I only get the mentors who can only use apple and not apple and linux. I need to search in some way that checks if apple is included in the

What is the purpose of Active Records?

坚强是说给别人听的谎言 提交于 2019-12-21 03:53:28
问题 I'm tinkering with CodeIgniter and have come across Active Records for the first time. At first I dismissed it as something for people who don't really know how to write SQL. I realise now that my analysis was flawed and Active Records are pretty prominent, especially in Rails. But what purpose do Active Records hold? Is it to abstract away from different RDBMS individualities. If so I thought that isn't that what SQL is meant to do. Furthermore what is best practice, should I be using these?

yii2 BaseActiveRecord findAll() conditions greater or less than

情到浓时终转凉″ 提交于 2019-12-21 03:42:58
问题 I have country database table (like found in the guide) that I test yii2 development application. I have field population and I want to create a public method in Country model to return all countries of specific population limits. i.e return all countries of population between x and y. I tried the following: // models/Country.php .... public function getPopulationBetween($lower, $upper) { return Country::findAll(['population' => [">=".$lower, "<=".$upper]]); } In the CountryController: public

Changing type of ActiveRecord Class in Rails with Single Table Inheritance

╄→尐↘猪︶ㄣ 提交于 2019-12-21 03:14:25
问题 I have two types of classes: BaseUser < ActiveRecord::Base and User < BaseUser which acts_as_authentic using Authlogic's authentication system. This inheritance is implemented using Single Table Inheritance If a new user registers, I register him as a User. However, if I already have a BaseUser with the same email, I'd like to change that BaseUser to a User in the database without simply copying all the data over to the User from the BaseUser and creating a new User (i.e. with a new id). Is

Changing type of ActiveRecord Class in Rails with Single Table Inheritance

流过昼夜 提交于 2019-12-21 03:14:12
问题 I have two types of classes: BaseUser < ActiveRecord::Base and User < BaseUser which acts_as_authentic using Authlogic's authentication system. This inheritance is implemented using Single Table Inheritance If a new user registers, I register him as a User. However, if I already have a BaseUser with the same email, I'd like to change that BaseUser to a User in the database without simply copying all the data over to the User from the BaseUser and creating a new User (i.e. with a new id). Is

Rails has_one with class name and foreign key

∥☆過路亽.° 提交于 2019-12-21 03:10:43
问题 I have a Rails model which I use two has_one relations: requester and friend . When in the console I use: f = FriendRequest.all f[0].requester I get ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.requester_id: SELECT "users".* FROM "users" WHERE "users"."requester_id" = 4 LIMIT 1 . I don't really know how to specify a `has_one' relationship with a class name and a key which specifies the record. This is my model: class FriendRequest < ActiveRecord::Base has_one

multiple database connections with has_many through

人走茶凉 提交于 2019-12-21 01:44:10
问题 How can I make a has_many through work with multiple database connections? I have a database named "master" that holds the location information. That is updated from a separate application. Users can have access to many locations, but all the other models are located in another database named "budget". Here are how the models are setup. # place.rb class Place < ActiveRecord::Base belongs_to :user belongs_to :location end # user.rb class User < ActiveRecord::Base has_many :locations, :through

Selecting columns with DISTINCT in PostgreSQL

元气小坏坏 提交于 2019-12-21 01:18:16
问题 I'm querying bus stops from a database, and I wish to have it only return 1 stop per bus line/direction. This query does just that: Stop.select("DISTINCT line_id, direction") Except that it won't give me any other attribute than those 2. I tried a couple of other queries to have it return the id in addition to the line_id and direction fields (ideally it would return all columns), with no luck: Stop.select("DISTINCT line_id, direction, id") and Stop.select("DISTINCT(line_id || '-' ||

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

半世苍凉 提交于 2019-12-20 21:26:08
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either