associations

ruby on rails - how to make relationship works in route, controller, view ? has_many, belongs_to

纵然是瞬间 提交于 2020-01-02 04:38:06
问题 I am struggling to get my relationship in rails to work. I have a User,Gallery,Comment model class Gallery has_many :comments belongs_to :user end class User has_many :comments has_many :galleries end class Comment belongs_to :gallery belongs_to :user end now what should i do in the routes, controller and views to link this all up ? please help me ? its quite confusing finding out the answers. If can, I dont want it to be nested like in the railscast, but i want for each model, eg gallery i

Matching nested model association attribute with includes

…衆ロ難τιáo~ 提交于 2020-01-02 04:08:31
问题 Suppose I have the following models: class Post < ActiveRecord::Base has_many :authors class Author < ActiveRecord::Base belongs_to :post And suppose the Author model has an attribute, name . I want to search for all posts with a given author "alice", by that author's name. Say there is another author "bob" who co-authored a post with alice. If I search for the first result using includes and where : post = Post.includes(:authors).where("authors.name" => "alice").first You'll see that the

Rails Filter records of child model based upon the parent model attribute

谁都会走 提交于 2020-01-01 19:47:47
问题 Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? 回答1: Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads

Rails Filter records of child model based upon the parent model attribute

你离开我真会死。 提交于 2020-01-01 19:47:27
问题 Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? 回答1: Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads

Nested Forms Rails

我只是一个虾纸丫 提交于 2020-01-01 12:33:09
问题 I have 2 models User and Address . class User < ActiveRecord::Base has_many :addresses accepts_nested_attributes_for :addresses end class Address < ActiveRecord::Base belongs_to :user end My controller def new @user = User.new @user.addresses << Address.new @user.addresses << Address.new end def create @user = User.new(params[:user]) if @user.save #do something else render 'new' end end And my View <%= form_for @user do |f| %> <%= f.label :name %> <%= f.text_field :name %> <%= f.fields_for

Datamapper: Sorting results through association

心已入冬 提交于 2020-01-01 09:17:13
问题 I'm working on a Rails 3.2 app that uses Datamapper as its ORM. I'm looking for a way to sort a result set by an attribute of the associated model. Specifically I have the following models: class Vehicle include DataMapper::Resource belongs_to :user end class User include DataMapper::Resource has n, :vehicles end Now I want to be able to query the vehicles and sort them by the name of the driver. I tried the following but neither seems to work with Datamapper: > Vehicle.all( :order => 'users

Rails 4: How to use includes() with where() to retrieve associated objects

烂漫一生 提交于 2020-01-01 07:52:32
问题 I can't figure out how to user the .where() method to retrieve associated model data. In this example, Projects belongs_to Users... class Project < ActiveRecord::Base belongs_to :user has_many :videos end class User < ActiveRecord::Base has_many :projects end class ProjectsController < ApplicationController def invite @project = Project.includes([:user]).where( {:hashed_id=>params[:id]} ).first end end In App/views/projects/invite.html.erg <%= debug( @project ) %> returns: --- !ruby/object

Rails 4: How to use includes() with where() to retrieve associated objects

大城市里の小女人 提交于 2020-01-01 07:52:12
问题 I can't figure out how to user the .where() method to retrieve associated model data. In this example, Projects belongs_to Users... class Project < ActiveRecord::Base belongs_to :user has_many :videos end class User < ActiveRecord::Base has_many :projects end class ProjectsController < ApplicationController def invite @project = Project.includes([:user]).where( {:hashed_id=>params[:id]} ).first end end In App/views/projects/invite.html.erg <%= debug( @project ) %> returns: --- !ruby/object

Android: Download file directly from browser

寵の児 提交于 2020-01-01 06:39:33
问题 I'm trying to make the android browser download a file of a specific type '.xxx’ say, so that I can then set an app to be associated with it. I've successfully done the association part, in that I've made it so that clicking on a file of the right type in an explorer app loads the appropriate app. I expected this to carry over to the browser so that if I attempted to download a file type .xxx then it would open the app. This doesn’t happen, all that happens is that the text contained in the

Rails - Parent/child relationships

六眼飞鱼酱① 提交于 2020-01-01 04:37:47
问题 I'm currently using a standard one-to-one relationship to handle parent/child relationships: class Category < ActiveRecord::Base has_one :category belongs_to :category end Is there a recommended way to do it or is this ok? 回答1: You will need to tweak the names you are using to get this working - you specify the name of the relationship, and then tell AR what the class is: class Category < ActiveRecord::Base has_one :child, :class_name => "Category" belongs_to :parent, :class_name => "Category