Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过附加属性使您拥有许多_Ruby On Rails_Ruby On Rails 3_Has Many Through - Fatal编程技术网

Ruby on rails 通过附加属性使您拥有许多

Ruby on rails 通过附加属性使您拥有许多,ruby-on-rails,ruby-on-rails-3,has-many-through,Ruby On Rails,Ruby On Rails 3,Has Many Through,我们如何通过关联在has_MUN中设置其他参数 谢谢。 尼尔什在这里遇到了同样的问题。在Rails 3中找不到任何如何使其动态工作的教程。 但是,您仍然可以通过连接模型本身获得所需的内容 p = Post.new(:title => 'Post', :body => 'Lorem ipsum ...') t = Tag.new(:title => 'Tag') p.tags << t p.save # saves post, tag and also add

我们如何通过关联在has_MUN中设置其他参数

谢谢。
尼尔什在这里遇到了同样的问题。在Rails 3中找不到任何如何使其动态工作的教程。 但是,您仍然可以通过连接模型本身获得所需的内容

p = Post.new(:title => 'Post', :body => 'Lorem ipsum ...')
t = Tag.new(:title => 'Tag')

p.tags << t
p.save   # saves post, tag and also add record to posttags table, but additional attribute is NULL now
j = PostTag.find_by_post_id_and_tag_id(p,t)
j.user_id = params[:user_id]
j.save   # this finally saves additional attribute
p=Post.new(:title=>'Post',:body=>'Lorem ipsum…)
t=Tag.new(:title=>'Tag')

p、 标签这篇博文有一个完美的解决方案:

该解决方案是:手动创建“:through model”,而不是在附加到所有者的数组时通过自动方式创建

使用该博客文章中的示例。您的模型所在的位置:

class Product < ActiveRecord::Base
  has_many :collaborators
  has_many :users, :through => :collaborators
end

class User < ActiveRecord::Base
  has_many :collaborators
  has_many :products, :through => :collaborators
end

class Collaborator < ActiveRecord::Base
  belongs_to :product
  belongs_to :user
end
类产品:协作者
结束
类用户:合作者
结束
类协作者
以前您可能已经离开:
product.collaborators当前用户:is\u admin=>true)


这种方法允许您在节省时间时设置附加参数。注意。如果模型尚未保存,
product.save
是必需的,否则可以忽略它。

我也遇到过类似的情况,我希望有一个联接表联接3个模型。但是我想从第二个模型中得到第三个模型ID

class Ingredient < ActiveRecord::Base

end

class Person < ActiveRecord::Base
  has_many :food
  has_many :ingredients_food_person
  has_many :ingredients, through: :ingredients_food_person
end

class Food
  belongs_to :person
  has_many :ingredient_food_person
  has_many :ingredients, through: :ingredients_food_person

  before_save do
    ingredients_food_person.each { |ifp| ifp.person_id = person_id }
  end
end

class IngredientFoodPerson < ActiveRecord::Base
  belongs_to :ingredient
  belongs_to :food
  belongs_to :person
end

起初我以为在我存钱之前,在分配了配料之后,我将无法获得配料。但是它会自动生成模型。

比如什么附加参数?我有一个模型post、一个join-model-PostTag和一个model-tag。我想指定是谁为帖子创建了关联的标记。@Codeglot关联模型本身可能具有超出两个链接对象id之外的其他属性。如果执行标记看起来可行,但有一种更简洁的方法,请参见我的答案:)
class Ingredient < ActiveRecord::Base

end

class Person < ActiveRecord::Base
  has_many :food
  has_many :ingredients_food_person
  has_many :ingredients, through: :ingredients_food_person
end

class Food
  belongs_to :person
  has_many :ingredient_food_person
  has_many :ingredients, through: :ingredients_food_person

  before_save do
    ingredients_food_person.each { |ifp| ifp.person_id = person_id }
  end
end

class IngredientFoodPerson < ActiveRecord::Base
  belongs_to :ingredient
  belongs_to :food
  belongs_to :person
end
food = Food.new ingredients: [Ingredient.new, Ingredient.new]
food.ingredients_food_person.size # => 2
food.save