I''m a bit unsure how to model a relationship in ActiveRecord. It''s a "has many through" with nested attributes. I already asked on SO but no luck so far. I would appreciate it if you could have a look and point me in the right direction. Feel to answer here or there! The question is here: http://stackoverflow.com/questions/9555888/what-active-record-association-do-i-use-in-this-scenario I won''t repeat myself, as the question does a good job of explaining, but I want to create users through accounts, and model the relationship through a relationship table. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QLs5TaQp1DoJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
if account have many user and user belongs_to account then this accepts_nested_attributes_for is write in account model. for more detail..see my github account https://github.com/vishalsingh/multiple_upload_photo .download it.hope this will work for you. On Sun, Mar 4, 2012 at 10:55 PM, Mohamad El-Husseini <husseini.mel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a bit unsure how to model a relationship in ActiveRecord. It''s a "has > many through" with nested attributes. I already asked on SO but no luck so > far. I would appreciate it if you could have a look and point me in the > right direction. Feel to answer here or there! The question is here: > http://stackoverflow.com/questions/9555888/what-active-record-association-do-i-use-in-this-scenario > > I won''t repeat myself, as the question does a good job of explaining, but > I want to create users through accounts, and model the relationship through > a relationship table. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/QLs5TaQp1DoJ. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 4 March 2012 17:25, Mohamad El-Husseini <husseini.mel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a bit unsure how to model a relationship in ActiveRecord. It''s a "has many through" with nested attributes. I already asked on SO but no luck so far. I would appreciate it if you could have a look and point me in the right direction. Feel to answer here or there! The question is here: http://stackoverflow.com/questions/9555888/what-active-record-association-do-i-use-in-this-scenario > > I won''t repeat myself, as the question does a good job of explaining, but I want to create users through accounts, and model the relationship through a relationship table.Better to repeat the question here. I cannot answer the questions without quoting it here which would mean I would have to copy bits from the link and paste them in here, which is too much effort. Also many will not be bothered to take the time to go and look somewhere else for your question. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin, I figured out most of it. But stayed stuck on a detail.
My app now has Account, User, and Role models. The Role model links the
Account and User models. User has_many roles, and has_many accounts through
roles. User accepts_nested_properties for accounts.
Everything works except for setting an extra attribute on the Role model.
The Role model has a role_type column that I want to set when the records
are created. It keeps showing up blank though.
Basically, when the User and the Account objects are created, I want the
Role model that joins them to have "owner" in the role_type column.
But
it''s showing up blank. I feel like I am mixing up "ownership"
with "roles",
yet it seems natural to store the in the same table. I thought I could use
inheritance to simplify it, but it''s causing more headaches. I
can''t change
the type column arbitrarily if I wanted to change the role of a use for
example.
My questions are: How do I set the role_type column? And is it stupid? It
doesn''t seem like a good way to set up something meant to be secure in
such
a way.
Should I use inheritance instead and create separate associations for each
role type?
Models:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation,
:accounts, :accounts_attributes
has_many :roles
has_many :accounts, through: :roles
accepts_nested_attributes_for :accounts
end
class Account < ActiveRecord::Base
attr_accessible :title
has_many :roles
has_many :users, through: "roles"
end
class Role < ActiveRecord::Base
attr_accessible :account_id, :user_id, :role_type
belongs_to :account
belongs_to :user
end
Controller:
def new
@user = User.new
@role = @user.roles.build(role_type: "owner")
@account = @user.accounts.build
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to root_path
else
render ''new''
end
end
View
<%= simple_form_for @user do |f| %>
<%= f.simple_fields_for :accounts do |account_form| %>
<%= account_form.input :title %>
<% end %>
<%= f.input :name %>
[snip]
<%= f.submit ''Sign up'', class: ''btn
btn-primary'' %>
<% end %>
On Tuesday, March 6, 2012 12:39:46 PM UTC-3, Colin Law
wrote:>
> On 4 March 2012 17:25, Mohamad El-Husseini
<husseini.mel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > I''m a bit unsure how to model a relationship in ActiveRecord.
It''s a
> "has many through" with nested attributes. I already asked on SO
but no
> luck so far. I would appreciate it if you could have a look and point me in
> the right direction. Feel to answer here or there! The question is here:
>
http://stackoverflow.com/questions/9555888/what-active-record-association-do-i-use-in-this-scenario
> >
> > I won''t repeat myself, as the question does a good job of
explaining,
> but I want to create users through accounts, and model the relationship
> through a relationship table.
>
> Better to repeat the question here. I cannot answer the questions
> without quoting it here which would mean I would have to copy bits
> from the link and paste them in here, which is too much effort. Also
> many will not be bothered to take the time to go and look somewhere
> else for your question.
>
> Colin
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/GOwMyhziDSYJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.