哪种做法更好?(which practise is better? about constructors [closed])
哪种做法更好,风格明智? 使用关键字“this”或为构造函数的参数选择不同的参数名称?
情况1:
public class Student { //variables private String name; //constructor public Student(String name) { this.name = name; } }
或情况2:
public class Student { //variables private String name; //constructor public Student(String aName) { name = aName; } }
Which between of the practises is better, style wise? using the keyword "this" or choosing a different parameter name for the parameter of the constructor?
case1:
public class Student { //variables private String name; //constructor public Student(String name) { this.name = name; } }
or case 2:
public class Student { //variables private String name; //constructor public Student(String aName) { name = aName; } }
原文:https://stackoverflow.com/questions/27126658
最满意答案
这看起来像一个简单的has_many和belongs_to关系:
# app/models/sponsor.rb class Sponsor < ActiveRecord::Base has_many :promo_codes # Zero or more. end # app/models/promo_code.rb #table has sponsor_id field class PromoCode < ActiveRecord::Base belongs_to :sponsor # Zero or one. end
has_one在这里并不合适,因为它会取代has_many:即,您有“has_many”和“belongs_to”或“has_one”和“belongs_to”。 has_one通常并不常用:当你已经拥有has_many关系并且不希望重构现有表时,通常会使用has_one。
This looks like a simple has_many and belongs_to relationship:
# app/models/sponsor.rb class Sponsor < ActiveRecord::Base has_many :promo_codes # Zero or more. end # app/models/promo_code.rb #table has sponsor_id field class PromoCode < ActiveRecord::Base belongs_to :sponsor # Zero or one. end
has_one isn't appropriate here, as it would replace has_many: ie, you either have "has_many" and "belongs_to" OR "has_one" and "belongs_to". has_one isn't generally used much: usually it is used when you already have a has_many relationship that you want to change to has_one, and don't want to restructure the existing tables.
相关问答
更多-
member = Member.first department = Department.find(member.department_id) 但是,由于在两个模型之间建立了关系,您可以简单地执行以下操作(我假设您在Member模型中具有belongs_to: :department ): member.department # will return the Department object member = Member.first department = Department.find(me ...
-
将其添加到您的模型 has_one :sender, :class_name => "User" has_one :recipient, :class_name => "User" 您可以调用@message.sender和@message.recipient ,并且都可以调用User模型。 而不是user:references您的生成命令中的user:references ,您需要sender:references和recipient:references Add this to your Model ...
-
Rails:引用潜在其他模型列表之一的模型(Rails: Model that references one of a list of potential other models)[2023-02-09]
感谢spickermann的评论。 多态协会做了伎俩! 这就是我所做的: class Service < ApplicationRecord belongs_to :serviceable, polymorphic: true ... end module ServiceTypes class ServiceType1 < ApplicationRecord has_one :service, as: :serviceable end end Thanks to spickerm ... -
这应该工作: Sequel.migration do change do create_table(:disputes) do primary_key :id foreign_key :accuser_id, :users foreign_key :defendant_id, :users end end end class Dispute < Sequel::Model many_to_one :accuser, :class=>:User ...
-
这实际上比你想象的更常见。 这是一个解决方案: class TasksController < ApplicationController before_filter :get_project before_filter :get_tasks private def get_project @project = Project.find(params[:project_id]) if params[:project_id] end def get_tasks @tas ...
-
这看起来像一个简单的has_many和belongs_to关系: # app/models/sponsor.rb class Sponsor < ActiveRecord::Base has_many :promo_codes # Zero or more. end # app/models/promo_code.rb #table has sponsor_id field class PromoCode < ActiveRecord::Base belongs_to :sponsor # Z ...
-
你可以把它们归nil def self.filter(type =nil, min=nil, max=nil, start_at=nil, end_at=nil) a = YourModel a = a.where(type => min..max) if min.present? && max.present? a = a.where(first_date: start_at..end_at) if start_at? && end_at.present? a = a.order("#{t ...
-
Rails中的模型4在不包含它们时验证可选关联(Model in Rails 4 validating optional associations when they are not included)[2023-12-30]
您需要条件验证和自定义属性。 请参阅: http : //edgeguides.rubyonrails.org/active_record_validations.html#conditional-validation 。 这样做会从控制器中获取验证代码并返回到它所属的模型中。 基本上类似于此(示例未经测试)应该起作用: validates :frequency, :presence => true, :if => :has_recurrence def has_recurrence=( yesorno= ... -
Rails FactoryGirl Factory具有可选的模型关联(Rails FactoryGirl Factory with optional model association)[2021-05-28]
您可以在工厂中使用回调。 这是一个例子: FactoryGirl.define do factory :coupon do code { rand(25**25) } percent_discount { rand(100**1) } start_at { Time.now } end_at { 30.day.from_now } after(:create) do |c| #do job related stuff end end en ... -
将possible_attribut e定义为attr_accessor和attr_accessibl e class MyModel attr_accessor :possible_attribute attr_accessible :possible_attribute end 现在你可以做到 m = MyModel.new(:possible_attribute => "value") m.possible_attribute #value 和 m.save 不会保存possible_a ...