activerecord

Multiple has_many relationships to same model

倾然丶 夕夏残阳落幕 提交于 2020-01-12 03:26:14
问题 I have a model User that can create Posts User has_many :posts Post belongs_to :user However, I want to also allow users to save posts as bookmarks. So I added the following: Bookmark belongs_to :post belongs_to :user User has_many :posts has_many :posts, :through => :bookmarks Post belongs_to :user has_many :posts, :through => :bookmarks This can't be right because it is now ambiguous when I do @user.posts . Does that refer to the posts the user wrote or the posts the user bookmarked? How do

Pros/Cons of storing serialized hash vs. key/value database object in ActiveRecord?

吃可爱长大的小学妹 提交于 2020-01-12 02:56:44
问题 If I have several objects that each have basically a Profile , what I'm using to store random attributes, what are the pros and cons of: Storing a serialized hash in a column for a record, vs. Storing a bunch of key/value objects that belong_to the main object. Code Say you have STI records like these: class Building < ActiveRecord::Base has_one :profile, :as => :profilable end class OfficeBuilding < Building; end class Home < Building; end class Restaurant < Building; end Each has_one

How to display error messages in a multi-model form with transaction?

偶尔善良 提交于 2020-01-11 13:05:08
问题 Two models, Organization and User, have a 1:many relationship. I have a combined signup form where an organization plus a user for that organization get signed up. The problem I'm experiencing is: When submitting invalid information for the user, it renders the form again, as it should, but the error messages (such as "username can't be blank") for the user are not displayed. The form does work when valid information is submitted and it does display error messages for organization, just not

How to display error messages in a multi-model form with transaction?

时光毁灭记忆、已成空白 提交于 2020-01-11 13:03:57
问题 Two models, Organization and User, have a 1:many relationship. I have a combined signup form where an organization plus a user for that organization get signed up. The problem I'm experiencing is: When submitting invalid information for the user, it renders the form again, as it should, but the error messages (such as "username can't be blank") for the user are not displayed. The form does work when valid information is submitted and it does display error messages for organization, just not

Exclude some ids from result in Rails ActiveRecord

好久不见. 提交于 2020-01-11 10:54:14
问题 I have following statement for query articles from some sections Article.all(:joins => :sections, :conditions => { :sections =>{ :id => [3, 4, 6, 7, 8, 9] }, :id_not_in => @some_ids }, :limit => 4) Variable @some_ids is array with ids of articles wich must be excluded from result. 回答1: If Article has_many :sections , try: Article.find(:all, :joins => :sections, :conditions => ["sections.id IN (?) AND id NOT IN (?)", [1,2,3], @some_ids], :limit => 4) 回答2: Article.all(:joins => :sections,

Rails: How to get objects with at least one child?

僤鯓⒐⒋嵵緔 提交于 2020-01-10 21:22:54
问题 After googling, browsing SO and reading, there doesn't seem to be a Rails-style way to efficiently get only those Parent objects which have at least one Child object (through a has_many :children relation). In plain SQL: SELECT * FROM parents WHERE EXISTS ( SELECT 1 FROM children WHERE parent_id = parents.id) The closest I've come is Parent.all.reject { |parent| parent.children.empty? } (based on another answer), but it's really inefficient because it runs a separate query for each Parent .

Rails: How to get objects with at least one child?

那年仲夏 提交于 2020-01-10 21:22:35
问题 After googling, browsing SO and reading, there doesn't seem to be a Rails-style way to efficiently get only those Parent objects which have at least one Child object (through a has_many :children relation). In plain SQL: SELECT * FROM parents WHERE EXISTS ( SELECT 1 FROM children WHERE parent_id = parents.id) The closest I've come is Parent.all.reject { |parent| parent.children.empty? } (based on another answer), but it's really inefficient because it runs a separate query for each Parent .

Rails: How to get objects with at least one child?

ⅰ亾dé卋堺 提交于 2020-01-10 21:22:06
问题 After googling, browsing SO and reading, there doesn't seem to be a Rails-style way to efficiently get only those Parent objects which have at least one Child object (through a has_many :children relation). In plain SQL: SELECT * FROM parents WHERE EXISTS ( SELECT 1 FROM children WHERE parent_id = parents.id) The closest I've come is Parent.all.reject { |parent| parent.children.empty? } (based on another answer), but it's really inefficient because it runs a separate query for each Parent .

Ruby On Rails - many to many between the same table

ε祈祈猫儿з 提交于 2020-01-10 20:10:10
问题 I am trying to create a somewhat complex relationship in Rails, and am having some trouble finding the best way to do so. I have a Users table in which each user acts as a teacher and a student. I would like to have a has_many "students" (which are also just Users) and a has_many "teachers" (which are also just Users). I do not want to do any subclassing or single table inheritance. I just want two different many_to_many's between Users. What is the best way to do this? Is this a bad idea to

PostgreSQL, Rails and :order => problem

為{幸葍}努か 提交于 2020-01-10 19:38:22
问题 I have the following line in my ActiveRecord model: class Record < ActiveRecord::Base has_many :users, :through => :record_users, :uniq => true, :order => "record_users.index ASC" This is intended to enable me to read out record.users in a way that I order using an index field in the record_users model. The problem is that this fails on PostgreSQL with the following error: ActionView::TemplateError (PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list Is there