has-many

Resetting position attribute when using acts_as_list gem? (Rails)

隐身守侯 提交于 2019-12-11 09:36:59
问题 I have a simple Rails 3 app where a Goal has many Objectives. In my views, I allow the user to-reorder the Objectives for any given Goal, and I am using the acts_as_list gem to achieve some of this functionality. Goal model: class Goal < ActiveRecord::Base attr_accessible :name, :description has_many :objectives, :order => :position, :dependent => :destroy end Objective model: class Objective < ActiveRecord::Base attr_accessible :name, :description, :position belongs_to :goal acts_as_list

deleteRecord with multiple belongsTo relationships in ember-cli

依然范特西╮ 提交于 2019-12-11 08:43:43
问题 What is the ember-cli best practice to deleteRecord() on a model that belongsTo multiple models? Do I have to manually clean up relationships on the parents? Migrating from ember to ember-cli I am having new trouble with deleteRecord() for a model 'star' that belongsTo multiple models, 'post' and 'user'. Before moving to ember cli it was working with this solution. The previous solution's delete action fails in the current ember-cli with errors and never calls the api . TypeError: Cannot read

Question on Proper Associations in Rails

孤街醉人 提交于 2019-12-11 08:34:44
问题 Take for example this situation. You have 3 models: Poet - Represents the author of a poem Poem - Represents a poem written by a Poet Printing - Represents a printed publication of any sort containing the Poet's Poem. Right off the bat poet and poem are obvious: Poet has_many poems Poem belongs_to poet It becomes confusing for me when dealing with the Printing model. In this case a Printing, in some sense, belongs to both the poet and poem. You could say a poet has_many printings or a poem

Rails: How do I transactionally add a has_many association to an existing model?

霸气de小男生 提交于 2019-12-11 02:37:09
问题 Let's imagine I run an imaginary art store with a couple models (and by models I'm referring to the Rails term not the arts term as in nude models) that looks something like this: class Artwork < ActiveRecord::Base belongs_to :purchase belongs_to :artist end class Purchase < ActiveRecord::Base has_many :artworks belongs_to :customer end The Artwork is created and sometime later it is included in a Purchase . In my create or update controller method for Purchase I would like to associate the

Scope on each last elements of a has_many relation

耗尽温柔 提交于 2019-12-10 18:35:12
问题 Let's say I have a has_many relation between User and Messages. I'd like to set a scope to be able to filter users by the ones who have something in the last message they posted. So searching only among each user's last message. Below I got the results among all messages... class Contact < ActiveRecord::Base has_many :messages scope :filter_by_last_messages, lambda { |value| joins(:messages).where("messages.content = ?", value) } end 回答1: Doing this in one shot in a scope is not possible but

ror - include foreign key at both ends of has_many and belongs_to?

假装没事ソ 提交于 2019-12-10 17:15:28
问题 I'm inheriting code which has: class Graphic < ActiveRecord::Base has_many :comments, :foreign_key => 'asset_id', :conditions => 'asset_type_id = 5', :order => 'created_at', :dependent => :destroy class Comment < ActiveRecord::Base belongs_to :graphic, :foreign_key => :asset_id It seems to me like the has_many should not have the foreign_key (it's referenced in the belongs_to ok I believe) but I am not sure, do you know? i.e. should it be class Graphic < ActiveRecord::Base has_many :comments,

I have a has_many relationships and I want to set custom limit and offset. as well as to count them

我是研究僧i 提交于 2019-12-10 13:53:33
问题 Hy, My code: @profile.images and i would like to get only 10 images at time and with a 10 offset, like this @profile.images(:limit => 10, :offset => 10) and not like this has_many :images, :limit => 10, :offset => 10 Then I would like to count in someway all the images for that profile. @profile.count_images Thanks (: has_many :images, :foreign_key => 'on_id', :conditions => 'on_type = "profile"' do def paginate(page = 1, limit = 10, offset = nil) page = nil if page < 1 limit = 1 if limit < 1

Rails - Enumerable Group_By multiple associations

假如想象 提交于 2019-12-10 11:16:16
问题 I want to group a collection of objects by their has many relations... like this s.inventoryitems.group_by{|i| i.locations} For the sake of simplicity this returns me something like this: {[1, 2, 3]=>["a"], [2]=>["b", "c"], []=>["d"]} I'm looking for a result like this though: {[1] => ["a"], [2] => ["a","b","c"], [3] => ["a"], [] => ["d"]} I am working on restructuring things so this can all be done in a more intuitive DB & model association oriented way, but in the meantime I need implement

Rails: has_many with extra details?

ぐ巨炮叔叔 提交于 2019-12-09 23:53:33
问题 While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models: class Ingredient < ActiveRecord::Base # ingredient name, end class Recipe < ActiveRecord::Base

has_many and single table inheritance

心已入冬 提交于 2019-12-09 12:35:48
问题 I have a has_many relationship between two entities, Feeds and Posts. I also have specific types of posts, Videos and Photos. This is structured in the database using single table inheritance. Right now I have my Feed model specifying a has_many relationship between Feeds and Posts (including the subtypes) class Feed < ActiveRecord::Base has_many :posts has_many :photos has_many :videos Is there a better, more conventional way to specify this? Or is what I have as simple as it can get? 回答1: