Menu
  • HOME
  • TAGS

rails Unizillaize constant in Association

ruby-on-rails,ruby-on-rails-4,model-associations

Models must be named in singular, not plural. class Appointment < ActiveRecord::Base ...

Can a Rails model be associated with the column name of another model?

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....

rails 4: active record associations: how to list all tasks from a project?

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...

association between two models

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 =...

How to add stuff I associated to migrations?

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 ...

Rails retrieving associated information

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 %> ...

Related object not saved

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...

has_many of the same table through several others

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...

Updating an association between existing records

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....

Creating a feed => NoMethodError: undefined method for ActiveRecord_Associations_CollectionProxy

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...

How to obtain different type of collections for the same Model in an Association in Rails 4?

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',...

“Create” Action for nested resource with multiple Associations in Rails

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 ...

Establishing a parent-child-relationship in object oriented design

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...

Laravel 4 : model assocation (belongsTo)

php,laravel,eloquent,model-associations

Try with eager loading: $foods = Food::with('FoodCategory')->get(); ...

'Categories' show action isn't displaying associated 'Listings' (Ruby on Rails)

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....

Display content through ActiveRecord_Association_CollectionProxy

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> ...

Rails 4 - How do I structure this model relationship?

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”...

Rails 4: Difference between validates presence on id or association

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...

Retrieving deeply nested ActiveRecord associations

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...

Relationship of SQL and NoSQL entities in Symfony [closed]

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...

rails simple_form using virtual attributes

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...

Rails: Associating two different models when not using params[:id]

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...

get count of has_many association in rails query

mysql,ruby-on-rails-3,model-associations

@post = Post.joins(:comments).select('posts.*, count(comments.id) as comment_count').group("posts.id").first ...

Showing attributes from a different rails model

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....

Rails association, I want something but the other model shouldn't care

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 ...

How to associate user with progress in lessons

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 ...

Laravel 5 - Updating associated models using push()

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...

Rails 4 - associations between controllers, id not getting passed

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...

Flask/SQLAlchemy - Difference between association model and association table for many-to-many relationship?

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...

How to implement the class - Student relationship in c#? [closed]

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...

Porting complicated has_many relationships to Rails >4.1 (without finder_sql)

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}' } } ...

Models association help in CakePHP

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...

Does rails association prevents a new record to be created like a foreign_key on a SQL create table would?

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...

has_one nested attributes not saving

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...

MySQL delete and remove from many-to-many table in one transaction

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....

Displaying nested association in view in Rails 4

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...

How to Join one table to another table by zipcode?

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......