ruby-on-rails,ruby,view,model-associations
<%= render partial: 'listings/list', locals: { listings: @category.listings } %> Makes listings avalable via listings var, not @listings, as in your template. Just remove @ sign....
ruby-on-rails,collections,rails-activerecord,model-associations
You could simply change the display part to the following <% Product.all.each do |product| %> . . . <ul class="microposts"> <%product.microposts.each do |micropost| %> <li><%=micropost.content%></li> <% end %> </ul> ...
ruby-on-rails,devise,rails-activerecord,model-associations
Everything looks fine. It would be easier to access parts through lessons for progresssions. Progression has_one :lesson has_many :parts, through: :lesson ...
mysql,cakephp-2.0,model-associations
In your Zipcode Model you need to: public $primaryKey = 'zip'; On a side note, why don't you have an autonumeric PK in that table? Zip codes usually contain non-numeric characters......
ruby-on-rails,activerecord,model-associations,nested-resources
It turns out this is much easier than it seemed. This is how I ended up doing it: class CommentsController < ApplicationController before_filter :findParent, only: [:create] [...] def create @comment = current_user.comments.build(comment_params) @comment.item = @item if @comment.save redirect_to @item else session[:errorbj] = @comment redirect_to @item end end ...
ruby-on-rails,associations,model-associations
class Kick < ActiveRecord::Base has_many :offs has_many :retailers, :through => :offs end class Retailer < ActiveRecord::Base has_many :offs has_many :kicks, :through => :offs end class Off < ActiveRecord::Base belongs_to :kick belongs_to :retailer end also make sure you properly indexed the models in db...
php,laravel,eloquent,model-associations
Try with eager loading: $foods = Food::with('FoodCategory')->get(); ...
ruby-on-rails,view,model-associations
You should be able to achieve what you want with this loop: <%= @companyaccount.each do |companyaccount| %> <%= companyaccount.company.name %> <%= companyaccount.number %> <% end %> ...
ruby-on-rails-4,model-associations,slug,friendly-id
You'll want to create the post the same way you were creating posts before. The fact that you want to be able to see a post individually, unconnected to an author, doesn't need to change how you created them. So, for example, for the url /author_slug/posts/new, your controller actions might...
mysql,ruby-on-rails-3,model-associations
@post = Post.joins(:comments).select('posts.*, count(comments.id) as comment_count').group("posts.id").first ...
ruby,ruby-on-rails-4,activerecord,model-associations
Try using models/provider.rb belongs_to :designation Then in models/designation.rb has_many :providers It may feel a little strange but the belongs_to just lets you know which model the id column needs to go in. In this case the provider model, so you'll need: rails g migration add_designation_id_to_provider designation_id:integer ...
ruby-on-rails,associations,model-associations
The error clearly says that @owner is nil. At BooksController you need to get the @owner object. As Owner and Books has nested association. So you need to define the routes like: resources :owners do resources :books end At BooksController class BooksController < ApplicationController do def create @owner=Owner.find(params[:id]) @book =...
ruby-on-rails,has-many-through,model-associations
If there is no activerecord model for ServicesUsers, try this @user = User.find(params[:user_id]) @user.services << Service.find(params[:service_id]) If the user/house is not already present, use create. or better yet, use find_or_create_by alternate way to model your requirement. http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association - This is exactly what you need. assemblies = services, parts = users....
sql,ruby-on-rails,associations,model-associations
No - it doesn't automatically do any validation in rails, and it doesn't add any database validations either. If you wanted you could validate it yourself: class Task < ActiveRecord::Base belongs_to :group validate :group_exists? def group_exists? !!self.group_id && Group.exists?(:id => self.group_id) end end There are gems which can help with...
ruby-on-rails,rails-activerecord,has-many-through,model-associations,rails-postgresql
subscribed_tags - it's a scope (where(user: self)), you can call where or join on them but not an item methods. In your case you want to use a scope class Card scope :with_subscription, -> { joins(tags: :subscriptions) } end # In controller current_user.cards.with_subscription.order('cards.created_at DESC') You can imagine current_user.cards like another...
ruby-on-rails,model-associations
You should set product's category in create action: def create @product = @category.products.new(product_params) # ... end in new action, you should just have def create @product = Product.new end Of course, you need to set @category (@category = Category.find(params[:category_id])) instance variable before (in before_filter, for example). You should also remove...
ruby-on-rails,ruby,rails-activerecord,model-associations,strong-parameters
Solution You need to change few things here. Firstly: = simple_fields_for @project.project_pipeline do |i| When you pass the object, rails have no idea it is to be associated with the parent object and as a result will create a field named project[project_pipeline] instead of project[project_pipeline_attributes]. Instead you need to pass...
ruby-on-rails,model-associations,activemodel
This is still fundamentally a has_and_belongs_to_many relationship, with a join table: +-------+ +-----------------+ +------------+ | Tasks |-----| GuidelinesTasks |-----| Guidelines | +-------+ +-----------------+ +------------+ In reality, your model is that a guideline has many tasks as well as tasks having many guidelines. The fact that a guideline “belongs to many”...
ruby-on-rails,ruby,activerecord,associations,model-associations
You could try using activemodel serializers to include associated records. class ProjectSerializer < ActiveModel::Serializer attributes :id has_many :groups end class GroupSerializer < ActiveModel::Serializer attributes :id has_many :members end You can check it out over at: https://github.com/rails-api/active_model_serializers The has_many, has_one, and belongs_to declarations describe relationships between resources. By default, when you...
ruby-on-rails,activerecord,model-associations
You're working with connection object. Remember it. Your problem is that you call find_by... method in has_many association. It returns one record BUT Idea model has no ruby attribute link to that. See here why. That's why Idea#save cannot call IdeaConnection#save (remember that for cascade saving connections realtion must have...
php,mysql,transactions,many-to-many,model-associations
Don't you try to update your image_tag with invalid id? ..null, 0, -1 or something else? Look to your code. I suppose $img and $values->image are not the same, but you load $img only when $values->image is provided. However, you try to update image_tag every time....
ruby-on-rails,activerecord,model-associations,finder-sql
Pass the proc that contains the SQL string as scope. has_many :internal_messages, -> { proc { "SELECT DISTINCT(internal_messages.id), internal_messages.* FROM internal_messages " + ' LEFT JOIN internal_messages_recipients ON internal_messages.id=internal_messages_recipients.internal_message_id' + ' WHERE internal_messages.sender_id = #{id} OR internal_messages_recipients.recipient_id = #{id}' } } ...
cakephp,model,model-associations
You need to have 3 database tables users projects - This table needs to have a column (administrator) which contains the user_id representing the user who is the administrator usersprojects - This table is cross reference table between the users and projects representing the members of a project CakePHP Models...
c#,class,model-associations,relationships
I hope it is on your experience level: ClassRoom public class ClassRoom { private List<Student> students = new List<Student>(); public bool Contains(Student student) { return this.students.Contains(student); } public void Add(Student student) { if (!Contains(student)) this.students.Add(student); student.StudentClassRoom = this; } public void Remove(Student student) { // if this contains, remove it...
c#,oop,design,composition,model-associations
In most circumstances it would be rather unrealistic to assume that a Student will be able to attend a Course without fulfilling any requirements established by the Course. A more complete protocol would start with a Student applying to a Course. Then the Course would accept or reject the application...
flask,sqlalchemy,many-to-many,flask-sqlalchemy,model-associations
My apologies, I finally stumbled across the answer in the SQLAlchemy docs... http://docs.sqlalchemy.org/en/rel_1_0/orm/basic_relationships.html#many-to-many ...where they explicitly define the difference: Many to Many adds an association table between two classes. association_table = Table('association', Base.metadata, Column('left_id', Integer, ForeignKey('left.id')), Column('right_id', Integer, ForeignKey('right.id')) ) The association object pattern is a variant on many-to-many: it’s...
ruby-on-rails,ruby,associations,model-associations
class Task < ActiveRecord::Base has_many :task_owners, dependent: :destroy has_many :task_supervisors, dependent: :destroy has_many :owners, through: :task_owners, source: :users has_many :supervisors, through: :task_supervisors, source: :users end You should be able to do this. Then you should get your task.owners and task.supervisors Edit: You will need to change your user model to...
ruby-on-rails,validation,ruby-on-rails-4,model-associations
Investigating further, I found that the 'presence' validator resolves to 'add_on_blank': http://apidock.com/rails/ActiveModel/Errors/add_on_blank def add_on_blank(attributes, options = {}) Array(attributes).each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) add(attribute, :blank, options) if value.blank? end end This does what it says: adds a validation error if the property in question is blank? This means it's...
mysql,mongodb,symfony2,doctrine,model-associations
Not possible if you want to have as strong a relation as e.g. foreign key constraints. The two databases will never have immediate knowledge of each other's data, transactions (!), scopes, etc. You can carefully write code that synchronizes the two. One approach I find easy and reliable: (for the...
ruby-on-rails,data-modeling,model-associations
Looking at the documentation here: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html accepts_nested_attribute would enable you to create corrections for a given report at the same time that you create the report. But in your case you will create corrections only once the report has been created so i don't think you need to use accepts_nested_attribute....
ruby-on-rails,ruby-on-rails-4,devise,model-associations
I was able to find a different method that seems to be working well at the moment. Instead of adding @company = Company.find (params[:id]), I was able to call @document.company.company_name within the show action. I'll forgo to the new and edit for the moment since show was all that mattered....
laravel,model-associations,laravel-5
The push method loops through all of the loaded relationships on the model and calls push on them, as well. So, it isn't the push on the $volunteer that's failing, it is a push on a related model that is failing. In Laravel 4, push has the following code: foreach...
ruby,model-associations,sequel,padrino
I'm not sure if that is what you want, but as you can see in the documentation, the :orders table should contain a foreign key to :users: Sequel.migration do change do create_table :orders do primary_key :id foreign_key :user_id, :users #no stuff yet end end end ...
ruby-on-rails,ruby,rails-activerecord,models,model-associations
Assuming your Job can only have one worker that is set when a Proposal is accepted, you'd want something like this: class Job < ActiveRecord::Base belongs_to :poster, class_name: 'User', foreign_key: 'poster_id' belongs_to :worker, class_name: 'User', foreign_key: 'worker_id' has_many :proposals, dependent: :destroy end class User < ActiveRecord::Base has_many :posted_jobs, class_name: 'Job',...
ruby-on-rails-4,autocomplete,simple-form,model-associations,virtual-attribute
You are referencing an association that doesn't exist with f.association :head_coach_name. Try this instead: f.association :head_coach, label_method: :head_coach_name You have a few other oddities in your code, including your head_coach_name definition, which should rely on head_coach instead of user. Same concern for the head_coach_name= method. Example. You have: def head_coach_name...
ruby-on-rails,ruby,rails-activerecord,model-associations
In your routes file, put something like this: resources :projects do resources :tasks end That will give you a route (among others) that looks like this: project_tasks_path(project_id) Which equates to: /projects/:project_id/tasks That route will give you all the tasks associated with that particular project. Of course, you still need to...
ruby-on-rails,ruby-on-rails-4,model-associations
Models must be named in singular, not plural. class Appointment < ActiveRecord::Base ...