Tag: 关系

将两个ActiveRecord :: Relation与OR组合,而不是AND,返回一个Relation而不是一个Array,以便稍后能够分页

a和b是ActiveRecord :: Relation对象,它们返回相同类型的对象(在本例中为Micropost对象) a.class => ActiveRecord::Relation b.class => ActiveRecord::Relation a.first => Micropost(…) b.first => Micropost(…) #They both return the same type of objects c=a+b c.class => Array #This is not what i’m looking for c=a|b c.class => Array #Not what i’m looking for either c=(a or b) c.class => ActiveRecord::Relation #But it is just a, […]

创建新记录时存储错误的user_id

所以刚刚得到了一些帮助,但现在有一个新问题……我正在尝试列出某个项目的所有’提交’(这些基本上都是简单的编辑)。 这当前正在工作,但是当我尝试列出创建提交的用户时,它显示错误的用户个人资料图片(或名称等)。 发生的事情是创建项目的人当前也是所有提交的user_id。 即,用户A创建项目…然后用户B来并添加提交。 但由于某些原因,它将用户A显示为所有提交的个人资料照片,我无法弄清楚原因。 这必须与提交的“新”或“创建”方法有关,因为它总是将新提交与创建项目的用户相关联(当它应该将提交与current_user关联时)。 在此先感谢任何帮助…… COMMITS控制器 class CommitsController @commit.user_id) first figure out how to set user_id on new commits (its nil now) @commits = Commit.paginate(page: params[:page]) end def create @project = Project.find(params[:project_id]) @commit = @project.commits.build(commit_params) if @commit.save flash[:success] = “You’ve successfully committed to this project” redirect_to project_path(@project) else render ‘new’ end end def […]

如何在Rails中建立相互友谊的模型

我知道之前在Stack Overflow上已经提出了这个问题,但答案并没有以我能解释的方式为我做。 我的一般方法受本教程的启发。 我正在尝试做的是为友好用户创建一个非常简单的模型,通过单个记录在两端创建相同的友谊。 在db级别,我只有一个’friendships’表,它只有一个user_id,一个friend_id和一个is_pending布尔列。 在user.rb中,我将关系定义为: has_many :friendships has_many :friends, through: :friendships 在friendship.rb中,我将关系定义为: belongs_to :user belongs_to :friend, :class_name => ‘User’ 如果我添加友谊,我可以访问如下: > a = User.first > b = User.last > Friendship.new(a.id, b.id) > a.friends => # 这是完美的,但我想要的是也能够像其他方向一样: > b.friends 不幸的是,随着关系的定义,我得到一个空集合。 运行的SQL显示它正在搜索user_id = b.id. 如何指定它还应搜索friend_id = b.id?

Rails has_many:通过关联

我正在尝试创建一个rails应用程序,用户可以在其中创建活动并邀请参与者加入并需要您的帮助! 我一直在圈子里,尝试一些东西,但似乎根本不对,这现在让我发疯! 我正在使用rails 4。 你会如何设置活动模型? User has_many :events through :meeting //for the participants? has_many :events // for the organizer? Event belongs to :user has_many :participants, class_name: “User” Participant belongs to :user has_many :events through :meeting Meeting has_many :participants has_many :events 这有任何意义吗? 我是否需要参与者模型,或者我只是过度设计它? 我想我有点困惑,组织者是一个用户,参与者也是用户和会议需要组织者和参与者所以不太清楚如何使这项工作… 另请阅读我只有在添加参与者时才能建立会议关系。 这会是要走的路吗? 谢谢!

2个用户之间的产品订单

我有三个模型:用户,产品,优惠以及这些模型之间关系的问题。 场景: 用户1发布产品 用户2可以向用户1发送价格为10美元的报价 用户1可以接受或拒绝该优惠 我现在的问题是: 用户,产品和优惠之间的正确关系是什么? 我该如何处理这些“接受或拒绝”的行为? 可能有更好的解决方案吗? 用户模型: class User :products end 产品型号: class Product :users end 优惠型号: class Offer < ActiveRecord::Base attr_accessible :offer_price, :status, :user_id, :product_id has_many :products has_many :users, through: :products end 提前致谢 :) 编辑: 我正在使用Rails 3.2.8

通过Rails中的belongs_to关系查询记录

我有一个活动模型,他们属于一个位置 如何选择location.country = Australia的所有活动? (例如) 我可以在范围内执行此操作吗?