Ruby on rails 保存具有多个关联的模型

Ruby on rails 保存具有多个关联的模型,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,这是我的问题,我有三个模型: 产品模型 class Product < ActiveRecord::Base attr_accessible :description, :title, :photo has_many :votes has_attached_file :photo, :styles => { :medium => "300x300" } before_save { |product| product.title = title.titleca

这是我的问题,我有三个模型:

产品模型

class Product < ActiveRecord::Base
  attr_accessible :description, :title, :photo
  has_many :votes

  has_attached_file :photo, :styles => { :medium => "300x300" }

  before_save { |product| product.title = title.titlecase }

  validates :title, presence: true, uniqueness: { case_sensitive: false }
  validates :photo, :attachment_presence => true

end
类产品{:medium=>“300x300”}
保存前{| product | product.title=title.titlecase}
验证:title、presence:true、惟一性:{区分大小写:false}
验证:photo,:attachment_presence=>true
结束
用户模型

class User < ActiveRecord::Base
    def self.from_omniauth(auth)
      where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.oauth_token = auth.credentials.token
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)
        user.save!
      end
    end
end
class用户
VoteModel

class Vote < ActiveRecord::Base
  belongs_to :product
  attr_accessible :user_id
end
类投票
现在我需要在VoteModel上保存一条带有ProductId和UserId的记录。但是我不知道怎么做,有人能帮我吗

更新


这是我的投票意见

<%= form_for @vote, :html => { :multipart => true } do |f| %>

    <%= f.label :user_id %>
    <%= f.text_field :user_id %>

    <%= f.label :product_id %>
    <%= f.text_field :product_id %>

    <%= f.submit "Crear Producto" %>
<% end %>

<%= link_to 'Cancel', root_path %>
{:multipart=>true}do | f |%>
这是控制器

class VotesController < ApplicationController

    def create
        @some_product = Product.find(params[:id])
        some_user = current_user
        vote = Vote.create(:user => some_user, :production => some_product)
        save!
    end

end
class VotesControllersome\u用户,:production=>some\u产品)
拯救
结束
结束

首先-应在所有模型中正确定义关联:

class Product < ActiveRecord::Base
  has_many :votes
  #...
  # bonus - to know who are the users who voted for the product
  has_many :users, :through => :votes
end

class User < ActiveRecord::Base
  has_many :votes
  #...
  # bonus - to know what products a user has voted on
  has_many :products, :through => :votes
end

class Vote < ActiveRecord::Base
  belongs_to :product
  belongs_to :user
  #...
end
从产品中访问投票

some_product.votes
访问从产品中投票的用户的步骤

some_product.users

所以你只想创建一个属于某个产品和某个用户的投票记录?只需使用一个标准表单(可能带有产品的隐藏_字段),然后根据当前_用户构建投票。我真的不明白你的错误或问题是什么。嗨,我试着在我的rails控制台上这样做:user=user.first product=product.first Vote.create(user,product),然后我得到了这个错误名称。错误:未定义的方法“stringify\u key”,它需要产品和用户的id。这需要投票。创建(user\u id:some\u user's\u id,product\u id:some\u product's\u id)感谢tamersalama,投票。创建(:user=>some\u user,:production=>some\u product)在我的投票控制器上执行创建操作,对吗?另外,如果你能帮我处理我非常欣赏的视图,我刚刚发布了控制器和投票的视图,因为我得到了这个错误:NilClass:classes的未定义方法'model_name',因为它是@vote的
表单
-参数[:id]将表示@vote的id。您应该使用的是
Product.find(参数[:vote][:production\u id])
。我知道您的表单只是一个临时表单(原型),因为我认为您不需要在文本字段中同时添加用户ID和产品ID。还有一件事:您应该像对待任何其他模型一样对待投票,即:您可以使用RailsScaffoldGenerator来构建基本的控制器/模型/视图,并在其上进行扩展。
some_product.users