has-many

limit the number of objects returned in a has_many

坚强是说给别人听的谎言 提交于 2019-11-29 06:24:52
How can I limit the number of rows returned in a has many relationship? For example: class User < ActiveRecord::Base has_many :photos end I want to be able to do: User.includes(:photos => {:limit => 8}).all This obviously doesnt work, but something with this functionality. Do I need to write out the SQL myself? Thanks in advance! EDIT: I dont want to limit the association, just the query results. So a User might have a thousand photos, I only want the top 3 returned. ronnieonrails Just add a limit option to the has_many association: class User < ActiveRecord::Base has_many :photos, :limit => 8

Rails: Non id foreign key lookup ActiveRecord

对着背影说爱祢 提交于 2019-11-29 00:39:43
问题 I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes , this rightly triggers SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2) (2 being the that coach's id here which is my problem.) I want it to trigger SELECT * FROM `coach_classes

Examples of new, create actions for has_many :through associations (nested)

那年仲夏 提交于 2019-11-28 22:11:26
I found this code on http://guides.rubyonrails.org/association_basics.html#the-has_one-through-association : class Document < ActiveRecord::Base has_many :sections has_many :paragraphs, :through => :sections end class Section < ActiveRecord::Base belongs_to :document has_many :paragraphs end class Paragraph < ActiveRecord::Base belongs_to :section end This is exactly what I'm trying to accomplish, but I'm still very new to Rails and was wondering if someone could show me a sample of the forms and the controllers needed in order to be able to create records for this setup? I was able to create

rails model has_many of itself

≯℡__Kan透↙ 提交于 2019-11-28 21:06:57
I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event.child_event or event.parent_event . But my googling hasn't worked out that well. My Model: class Event < ActiveRecord::Base attr_accessible :days_before, :event_name, :event_date, :list_id, :recipient_email, :recipient_name, :parent_event_id, :is_deleted, :user_id belongs_to :user has_many :event_email has_many :event end My Schema: create_table "events", :force => true do |t| t.datetime "event_date" t

ActiveAdmin with has_many problem; undefined method 'new_record?'

喜夏-厌秋 提交于 2019-11-28 20:44:31
问题 I'm trying to customise a ActiveAdmin form for a Recipe model that has a has_many relationship with Step. class Recipe < ActiveRecord::Base has_many :steps end class Step < ActiveRecord::Base acts_as_list :scope => :recipe belongs_to :recipe end I have the following in my ActiveAdmin file with relation to this: form do |f| f.has_many :steps do |ing_f| ing_f.inputs end end The following error is thrown when I try to load the form: undefined method `new_record?' for nil:NilClass I've isolated

Rails has_many with alias name

霸气de小男生 提交于 2019-11-28 15:07:49
In my User model I could have: has_many :tasks and in my Task model: belongs_to :user Then, supposing the foreign key 'user_id' was stored in the tasks table, I could use: @user.tasks My question is, how do I declare the has_many relationship such that I can refer to a User's Tasks as: @user.jobs ... or ... @user.foobars Thanks a heap. Sam Saffron Give this a shot: has_many :jobs, foreign_key: "user_id", class_name: "Task" Note, that :as is used for polymorphic associations . You could also use alias_attribute if you still want to be able to refer to them as tasks as well: class User <

Sequelize onDelete not working

会有一股神秘感。 提交于 2019-11-28 01:50:20
问题 I have associations between two models defined as below: For Contact Model (in a separate file) classMethods: { associate: function (models){ Contact.belongsTo(models.User) } } For User(in a separate file) classMethods: { associate: function (models){ User.hasMany(models.Contact, {onDelete: 'CASCADE'}) } } The problem is when deleting the user the contact related to the user is not deleting any advice on what am I doing wrong would be helpful? Thanks in advance 回答1: I think you need to define

limit the number of objects returned in a has_many

大兔子大兔子 提交于 2019-11-28 00:01:21
问题 How can I limit the number of rows returned in a has many relationship? For example: class User < ActiveRecord::Base has_many :photos end I want to be able to do: User.includes(:photos => {:limit => 8}).all This obviously doesnt work, but something with this functionality. Do I need to write out the SQL myself? Thanks in advance! EDIT: I dont want to limit the association, just the query results. So a User might have a thousand photos, I only want the top 3 returned. 回答1: Just add a limit

Need data from rails join table, has_many :through

ⅰ亾dé卋堺 提交于 2019-11-27 20:14:29
I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id . This would mean: class User has_many :things, :through => :follows end class Thing has_many :users, :through => :follows end class Follow belongs_to :users belongs_to :things end So I can retrieve thing.users with no problem. My issue is if in the follows table, I have a column named "relation", so I can set a follower as an "admin", I want to have access to that relation. So in a loop I can do something like: <% things.users.each do |user| %> <%= user

Rails has_many association count child rows

半世苍凉 提交于 2019-11-27 11:11:45
问题 What is the "rails way" to efficiently grab all rows of a parent table along with a count of the number of children each row has? I don't want to use counter_cache as I want to run these counts based on some time conditions. The cliche blog example: Table of articles. Each article has 0 or more comments. I want to be able to pull how many comments each article has in the past hour, day, week. However, ideally I don't want to iterate over the list and make separate sql calls for each article