associations

Rails: How do I route unassociated resources?

南笙酒味 提交于 2019-12-11 15:25:38
问题 I have four models: class User < ActiveRecord::Base has_many :posts end class Category < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user belongs_to :metric has_many :comments end class Comments < ActiveRecord::Base belongs_to :post end I'd like to be able to access a post within a certain category for a specific user. For example: http://domain.com/users/1/categories/1/posts Also I want to see a list of all categories if I visit: http://domain.com/users

nester attributes for has_many are not shown if the form fails validation

穿精又带淫゛_ 提交于 2019-12-11 15:14:13
问题 If form fails validation, fields for has_many association, disappears from view: my new action looks like def new @realty = Realty.new @realty.build_user @realty.build_address #user, address are has_one association, and they stay in view after failed validation 6.times { @realty.realty_images.build } # and this one vanishes away respond_to do |format| format.html # new.html.erb format.json { render json: @realty } end end if I add to my form this snippet - if @realty.realty_images.empty? - 6

How to restrict a view by

限于喜欢 提交于 2019-12-11 14:49:29
问题 I have successfully set up a friendship self referencial association for users in my Ruby on Rails app after following Ryan Bates' railscast. I can successfully follow, unfollow, and view who is following. Now I want to pull in another element and I am not sure how to do it. When visiting a user/show page of a user that the current_user is already friends with...what is the syntax for checking the Friendships table for an existing friendship. Here is how the associations are set up.

Aggregation or composition or simple association?

…衆ロ難τιáo~ 提交于 2019-12-11 14:39:32
问题 There is one example to explaining associations in UML. A person works for a company; a company has a number offices. But I am unable to understand the relationship between Person, Company, and Office classes. My understanding is: a company consists of many persons as employees but these classes exist independently so that is simple association with 0..* multiplicity on Person class' end a company has many offices and those offices will not exist if there is no company so that is composition

Associations with Chartkick & Rails

眉间皱痕 提交于 2019-12-11 12:39:49
问题 I want to use chartkick to create a line graph of records, which belong to a planned task. In other words: plantask has_many records Records has two fields I'm interested in graphing. The created_at, which will be my X-axis, and data(An integer), which will be my Y-axis. So far I've gotten pretty close. By inserting this into my view: <%= line_chart @plantask.records.group_by_day(:created_at).sum(:data) %> I can see that my x-axis is displaying perfectly. However, it appears that the y-axis

Ruby on Rails Join Table Associations

守給你的承諾、 提交于 2019-12-11 09:33:42
问题 I have a Ruby on Rails application setup like so: User Model has_and_belongs_to_many :roles Role Model has_many :transactions has_and_belongs_to_many :users Transaction Model belongs_to :role This means that a join table is used called roles_users and it also means that a user can only see the transactions that have been assigned to them through roles, usage example: user = User.find(1) transactions = user.roles.first.transactions This will return the transactions which are associated with

Doctrine/Symfony2 Relationship Between 2 Entities over 2 Forms

心不动则不痛 提交于 2019-12-11 09:25:44
问题 Current the following exists: Listing Entity class Listing { /** * @var integer $id * * @ORM\Column(name="listing_id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $title * * @ORM\Column(name="title", type="string", length=255) */ private $title; /** * @ORM\ManyToOne(targetEntity="IntA\UserBundle\Entity\User", cascade ={"delete"}, inversedBy="listings") * @ORM\JoinColumn(onDelete="CASCADE") */ protected $user; /** * @ORM\OneToMany

Ruby/Rails: friend_id is not added to friendships table

你离开我真会死。 提交于 2019-12-11 09:19:22
问题 Using the Rails 4.1.4 framework. Having a problem adding a 'friend_id' to the user_friendships table below... when the program runs in rails, I am redirected to the root (as expected) and given the success message that the friendship was created. However, when I crack open the database GUI, I see that only a user_id is saved into the user_id column, and the friend_id column is always left 'nil', no matter who friends who. I've searched high and low - I know it must be something simple and it

how to do a has_many relation on two tables?

情到浓时终转凉″ 提交于 2019-12-11 08:50:35
问题 I'm trying to do a has_many relation with a table on creation and also to add it in another table already created. I have my User table already with no relations. I want to create a Pro_user with a relation has_many User . The thing is that one User can have multiple Pro_user and a Pro_user can also also have multiple User . So both tables need a has_many right ? So what I thought of was that rails g model pro_user name:string email:string password_digest:string user:references But this is

Multiple relation with same model

情到浓时终转凉″ 提交于 2019-12-11 08:36:31
问题 i have two model 'Asset' , and 'User' .'Asset' can be assigned to one 'user' and 'asset' is created by one 'user'.here is detail of model classes class Asset < ActiveRecord::Base belongs_to :assigned_to ,:class_name=>'User' belongs_to :creator ,:class_name=>'User' end and class User < ActiveRecord::Base has_many :assets end now in assets show.html.erb i can access name of creator with @asset.creator.name but i can not see name of 'assigned_to' @asset.assigned_to.name #gives nothing both