Rails – check if record exists in has_many association
Try: current_user.items.exists?(params[:id]) Or current_user.items.exists?(@item.id)
Try: current_user.items.exists?(params[:id]) Or current_user.items.exists?(@item.id)
First: Foreign key relationship are not only for navigating. They mainly serve to ensure that no spurious values are introduced in the relationship. They also may help the database for query optimization. I would advise you to reconsider that. Anyway, for creating a query that uses several unrelated entities, you need to put them as … Read more
I’m going to ignore Aggregation. It is not a very clearly defined concept and in my opinion it causes more confusion than it is worth. Composition and Association are quite enough, Craig Larman told me so. It might not be the answer your instructor was looking for but it is unlikely to be implemented in … Read more
I’m not sure if there is anything built into rails that will do exactly what you want, but you could mimic the first_or_initialize code with a more concise extension than you are currently using, which I believe does what you want, and wrap it into a reusable extension such as the following. This is written … Read more
Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key references in the owned table to both parent tables? What if you have three parent types? It just doesn’t scale to large designs. A join-table decouples the join, so that the owned table … Read more
Rails 4 makes this too easy 🙂 Foo.where.not(id: Bar.select(:foo_id).uniq) this outputs the same query as jdoe’s answer SELECT “foos”.* FROM “foos” WHERE “foos”.”id” NOT IN ( SELECT DISTINCT “bars”.”foo_id” FROM “bars” ) And as a scope: scope :lonely, -> { where.not(id: Bar.select(:item_id).uniq) }
Yes, I think you’ve just found a slightly odd-looking scenario in Rails. I suppose it might be useful to view “status” as a sort of category to which the bug belongs — in that light, it makes sense.
The way is to define additional extending selector to has_many scope: class Customer < ActiveRecord::Base has_many :orders do def by_account(account) # use `self` here to access to current `Customer` record where(:account_id => account.id) end end end customers.orders.by_account(account) The approach is described in Association Extension head in Rails Association page. To access the Customer record in … Read more
In the ApplicationForm class, you need to specify ApplicationForms’s relationship to ‘form_questions’. It doesn’t know about it yet. Anywhere you use the :through, you need to tell it where to find that record first. Same problem with your other classes. So # application_form.rb class ApplicationForm < ActiveRecord::Base has_many :form_questions has_many :questions, :through => :form_questions end … Read more
Instead of replacing the collection (team.setUserTeamRoles(new HashSet<UserTeamRole>());) you have to clear() the existing one. This happens because if Hibernate loads the entity (and its collections) from DB, it “manages” them, ie. tracks their changes. Generally when using Hibernate it’s better not to create any setters for collections (lists, sets). Create only the getter, and clear … Read more