has-many

Rails has_many through aliasing with source and source_type for multiple types

核能气质少年 提交于 2019-12-03 06:51:58
So here is a sample class class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @company.angels and @company.vc_firms works as expected. But how would I have @company.investors that are comprised of both source types? That would work for all polymorphics on the investor column of the Investments table? or perhaps a way of using a scope to merge all source_type? Investment model looks like this: class Investment <

Delete a Has-Many Relationship ONLY

旧街凉风 提交于 2019-12-03 05:45:33
I have a: has_and_belongs_to_many :friends, :join_table => "friends_peoples". To add a friend I do: @people.followers << @friend which create the relationship and a new person profile. Now I'd like to delete the relationship ONLY and not the person profile. I tried @people.friends.delete(guilty.id) but it deletes the person profile and not the relationship. Any idea? Have you tried this? @people.friends.delete(guilty) 来源: https://stackoverflow.com/questions/3852017/delete-a-has-many-relationship-only

When will ActiveRecord save associations?

这一生的挚爱 提交于 2019-12-03 01:02:18
1) I know that it will save associations when autosave: true as per http://railsapi.com/doc/rails-v2.3.8/classes/ActiveRecord/AutosaveAssociation.html 2) I know that it will save associations that are constructed like book = Book.new(name: 'foo') book.authors.build(name: 'bar') #has_many book.save or like book = Book.new(name: 'foo') book.build_author(name: 'bar') #has_one book.save 3) I think associations are also saved when they are assigned or added book = Book.new(name: 'foo') book.author = Author.new(name: 'bar') book.save or book = Book.new(name: 'foo') book.authors << Author.new(name:

Grails dynamic scaffold with hasMany: is it a bug or am I misconfiguring?

自作多情 提交于 2019-12-03 00:31:07
I'm a Grails noob and running into something that seems to be a bug, but it is entirely possible I'm not configuring everything correctly. I've got two simple Domain Classes: class Player { String firstName String lastName static constraints = { firstName(blank:false) lastName(blank:false) } String toString() { lastName + ", " + firstName } } and class Team { String mascot; static hasMany = [players:Player] static constraints = { mascot(blank:false) } } I have controllers for both that do nothing beyond dynamic scaffold these two Domain Classes. But even when I have a list of Players in my DB,

Selecting all Records if Record has any of the ID's from Array

帅比萌擦擦* 提交于 2019-12-02 16:59:00
问题 Good Morning Overflow. I'm having a problem trying to select all Treatments that have any of the ID's stored in an array called @problem . Here is my Treatments controller. @problem = Remedy.find_by_sql(["SELECT id FROM remedies WHERE LOWER(\"remedyName\") LIKE?", "%#{params[:searchremedy]}%".downcase]) query = "SELECT * FROM treatments INNER JOIN remedies_treatments on treatments.id = remedies_treatments.treatment_id WHERE remedies_treatments.treatment_id LIKE ?" @pretreatments = Treatment

How to display highest rated albums through a has_many reviews relationship

那年仲夏 提交于 2019-12-02 10:00:42
I'm setting up a simple ruby/rails app where users can review albums. On an album's show page I average all the user reviews associated with that album through this code in my albums controller def show @album = Album.find_by_id(params[:id]) if @album.reviews.present? @ratings = Review.where(album_id: @album).average(:rating).truncate(2) else render 'show' end end This gives me an average rating for each album. On my home page (routed through a different controller) I want to display the top 7 albums with the highest average rating. What I originally did was put this code into the separate

CakePHP increment value

非 Y 不嫁゛ 提交于 2019-12-02 06:14:21
My problem looks as follows: I want to make a vote app where People can choose one or more Events(like Doodle). For this I have set up a function called vote. In the View you can choose the Events using checkboxes. The Models are Poll and Groupevent. A Poll has Many Groupevents. My Problem is when I call updateAll() , all values of the associated Groupevents are incremented. Here is my code: View: echo $this->Form->create('Poll', array('action' => 'vote')); for($i=0; $i<$count; $i++){ echo $this->Form->input('Groupevent.'.$i.'.votes', array('type'=>'checkbox', 'label'=>$this->Groupevent[

How to 'unlock' a field in a CakePHP form when it is part of a hasMany association

自闭症网瘾萝莉.ら 提交于 2019-12-02 05:59:00
问题 I have a form that represents a RewardModifier table in our database. That RewardModifier hasMany RewardOption . My form is structured like this (image): So, the RewardModifier can have many elements on the page, each with many RewardOption items. The Problem The problem is, that users can delete sections of this form using Javascript, which essentially removes it from the DOM. When they do that, it breaks the security component, because the POST'ed fields do not match the token supplied when

What's the rails way to load other models collections for new, edit update and create actions?

戏子无情 提交于 2019-12-02 04:13:19
How is the best way to load Category model, for ProductController in new, edit update and create actions Product has categories collection class Product < ActiveRecord::Base has_many :categories end Always for new, edit, create and update actions, I need to load the categories collection to populate a check_box_tag list The "baby steps" for this situation is: class Admin::ProductsController < Admin::AdminApplicationController def new @product = Product.new @categories = Category.all end def edit @product = Product.find(params[:id]) @categories = Category.all end def create @product = Product

How to 'unlock' a field in a CakePHP form when it is part of a hasMany association

半城伤御伤魂 提交于 2019-12-02 00:49:18
I have a form that represents a RewardModifier table in our database. That RewardModifier hasMany RewardOption . My form is structured like this (image): So, the RewardModifier can have many elements on the page, each with many RewardOption items. The Problem The problem is, that users can delete sections of this form using Javascript, which essentially removes it from the DOM. When they do that, it breaks the security component, because the POST'ed fields do not match the token supplied when the page was generated. Now, I have been using unlockedFields to handle this before: $this->Security-